Code Monkey home page Code Monkey logo

emberconf2020-tailwind-css-best-practices's Introduction

EmberConf 2020: Tailwind CSS Best Practices

Welcome!

You can view the training on YouTube:

โ†’ View the training on YouTube

Follow along using the instructions below.

Getting help

If you have any questions,

  • DM us (@samselikoff or @ryanto) in the Ember Community Discord
  • Email us at [email protected]

Running the training app on your computer

From a directory,

git clone [email protected]:embermap/emberconf-2020-tailwindcss-best-practices.git
cd emberconf-2020-tailwindcss-best-practices
yarn install
ember s

Intro

Quick overview.

Some useful VSCode plugins:

1: Basic Tailwind

  • Style a blog post
  • Pseudo states
  • Responsive design

2: Extract components, not classes

Padding trick for fixed aspect ratio.

<div class="relative bg-red-500 pb-2/3">
  <img
    class="absolute object-cover w-full h-full"
    src="https://source.unsplash.com/_Dogn_h7Qek"
  />
</div>

How to think about abstracting + sharing? Might reach for @apply.

Problem is, you still have to duplicate html structure. You need a wrapper + a child. Another problem is that you have to go to the css file and break html-first workflow.

Instead, use components.

<AspectRatio @ratio='16:9'>
  <img>
</AspectRatio>

This is going to be a theme of this training. Components like this keep us in the html. That should be a goal with the abstractions you make: html-first workflow. Keeps you productive.

3: Tailwind-friendly Component APIs

<Link> takes activeClass arg. Let's make it work.

Our styles are stomping each other. We need to think of an API that's Tailwind-friendly.

What we really want is <Link class='' @activeClass='' @inactiveClass=''>. Let's make it work.

4: Layout with Flexbox

Old school button group here. Buttons are foated left, parent is inline-block. How to center?

Use text-center.

This is weird - we're using text-align: center to lay out a component?

With Tailwind + modern css you'll get very familiar with flexbox. Its great because it works in many more contexts and you usually don't need to worry about whether the child you're laying out is block or inline. The layout is kept more separate from the thing you're laying out. Also floats are super weird. Also the height of our group is different from the buttons โ€“ because of line-height. Again, inline elements are kinda weird.

[ Exercise: Once you have it using flexbox, copy + paste the button group so there are two. Play with the justify-* classes on the parent. ]

5: Exercise: Practice Layout with Flexbox

Match the layout on the right. Notice the behavior if you shrink the viewport. You'll need to look up the "Flex Shrink" utilities on tailwindcss.com.

6: More layout - measured text

Try to encapsulate the measured text in a component. Notice how you can lay it out with flexbox.

7: Layout with Grid

Build with flexbox first. Then refactor to grid.

Grid is amazing. Gap is amazing.

8: Exercise: Practice with Grid

9: Working with SVG

Copy svgs in, get rid of hard-coded widths and heights. Set fill or stroke to currentColor.

10: Exercise: Practice with SVG

Copy svgs in, get rid of hard-coded widths and heights. Set fill or stroke to currentColor.

11: Form styling library

Forms by default aren't very "utility-friendly". There's also lots of inconsistencies across browsers.

The Custom forms plugin smoothes these out. Let's see how it works.

12: Writing a plugin for focus-visible

Polyfill: https://github.com/WICG/focus-visible

Download & import the polyfill

Impot plugin from Tailwind:

const plugin = require("tailwindcss/plugin");

Write the plugin.

plugin(function({ addVariant, e }) {
  addVariant("focus-visible", ({ modifySelectors, separator }) => {
    modifySelectors(({ className }) => {
      return `.${e(
        `focus-visible${separator}${className}`
      )}[data-focus-visible-added]`;
    });
  });
});

Add the focus-visible variant to the relevant plugins:

variants: {
  borderColor: ["responsive", "hover", "focus", "focus-visible"],
  boxShadow: ["responsive", "hover", "focus", "focus-visible"],
  zIndex: ["responsive", "focus", "focus-visible"]
},

13: Responsive designs for very different layouts

Finishing off with a hard lesson learned.

First, if a layout is very different between two breakpoints, just split it up.

Avoid JS device viewport width. Use CSS media queries. Robust to SSR.

Resources

emberconf2020-tailwind-css-best-practices's People

Contributors

alonski avatar samselikoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

emberconf2020-tailwind-css-best-practices's Issues

"ember s" command not found

To run the training app on my computer, the last step provided "ember s" return the message "ember: command not found." However, running "npm start" which runs the script "ember serve" does work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.