|
| 1 | +import { adamwathan } from '@/authors' |
| 2 | +import image from './card.jpg' |
| 3 | + |
| 4 | +export const meta = { |
| 5 | + title: 'Tailwind CSS v1.5.0', |
| 6 | + description: `Tailwind CSS v1.5.0 is here, now with component variants, responsive container variants, focus-visible support, and more.`, |
| 7 | + date: '2020-07-15T18:55:18.391Z', |
| 8 | + authors: [adamwathan], |
| 9 | + image, |
| 10 | + discussion: 'https://github.com/tailwindcss/tailwindcss/discussions/2033', |
| 11 | +} |
| 12 | + |
| 13 | +<!--excerpt--> |
| 14 | + |
| 15 | +I was hoping to save v1.5.0 for something _really_ exciting but we needed a new feature to support the new [`@tailwindcss/typography`](https://github.com/tailwindcss/typography) plugin so h\*ck it, we're dropping some new stuff on you early. |
| 16 | + |
| 17 | +No breaking changes, this is a minor release and we're professionals you silly goose. |
| 18 | + |
| 19 | +<!--/excerpt--> |
| 20 | + |
| 21 | +I was hoping to save v1.5.0 for something _really_ exciting but we needed a new feature to support the new [`@tailwindcss/typography`](https://github.com/tailwindcss/typography) plugin so h\*ck it, we're dropping some new stuff on you early. |
| 22 | + |
| 23 | +No breaking changes, this is a minor release and we're professionals you silly goose. |
| 24 | + |
| 25 | +## New Features |
| 26 | + |
| 27 | +### Component `variants` support |
| 28 | + |
| 29 | +Until Tailwind CSS v1.5.0, only "utility" classes were really intended to be used with `variants` (like "responsive", "hover", "focus", etc.) |
| 30 | + |
| 31 | +While these are still much more useful for utilities than any other type of class, we now support generating variants for component classes as well, like the `prose` classes in the new `@tailwindcss/typography` plugin: |
| 32 | + |
| 33 | +```html |
| 34 | +<article class="prose md:prose-lg"> |
| 35 | + <!-- Content --> |
| 36 | +</article> |
| 37 | +``` |
| 38 | + |
| 39 | +You can take advantage of this feature in your own component classes by using the new `variants` option in the second argumant of the `addComponents` plugin API: |
| 40 | + |
| 41 | +```js |
| 42 | +plugin(function ({ addComponents })) { |
| 43 | + addComponents({ |
| 44 | + '.card': { |
| 45 | + // ... |
| 46 | + } |
| 47 | + }, { |
| 48 | + variants: ['responsive'] |
| 49 | + }) |
| 50 | +}) |
| 51 | +``` |
| 52 | + |
| 53 | +...or using the array shorthand you might be familiar with from the `addUtilities` API: |
| 54 | + |
| 55 | +```js |
| 56 | +plugin(function ({ addComponents })) { |
| 57 | + addComponents({ |
| 58 | + '.card': { |
| 59 | + // ... |
| 60 | + } |
| 61 | + }, ['responsive']) |
| 62 | +}) |
| 63 | +``` |
| 64 | + |
| 65 | +To take advantage of these feature in your custom CSS (rather than using the plugin API), you can use a new `@layer` directive to explicitly tell Tailwind that your styles belong to the "components" bucket: |
| 66 | + |
| 67 | +``` |
| 68 | +@layer components { |
| 69 | + @responsive { |
| 70 | + .card { |
| 71 | + /* ... */ |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +This helps Tailwind purge your unused CSS correctly, ensuring it doesn't remove any responsive component variants when using the default "conservative" purge mode. |
| 78 | + |
| 79 | +### Responsive `container` variants |
| 80 | + |
| 81 | +Piggy-backing off of the new component `variants` support, the `container` class now supports variants! |
| 82 | + |
| 83 | +```html |
| 84 | +<!-- Only lock the width at `md` sizes and above --> |
| 85 | +<div class="md:container"> |
| 86 | + <!-- ... --> |
| 87 | +</div> |
| 88 | +``` |
| 89 | + |
| 90 | +We've enabled responsive variants by default, but if you are sick in the head you can also manually enable other variants like `focus`, `group-hover`, whatever: |
| 91 | + |
| 92 | +```js |
| 93 | +// tailwind.config.js |
| 94 | +module.exports = { |
| 95 | + // ... |
| 96 | + variants: { |
| 97 | + container: ['responsive', 'focus', 'group-hover'], |
| 98 | + }, |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### New `focus-visible` variant |
| 103 | + |
| 104 | +We've added support for the [`:focus-visible` pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible) using a new `focus-visible` variant. |
| 105 | + |
| 106 | +This is super useful for adding focus styles that _only_ appear to keyboard users, and are ignored for mouse users: |
| 107 | + |
| 108 | +```html |
| 109 | +<button class="focus-visible:outline-none focus-visible:shadow-outline ..."> |
| 110 | + Click me |
| 111 | +</button> |
| 112 | +``` |
| 113 | +
|
| 114 | +It's not enabled for anything by default, but you can enable it in the `variants` section of your config file: |
| 115 | +
|
| 116 | +```js |
| 117 | +// tailwind.config.js |
| 118 | +module.exports = { |
| 119 | + // ... |
| 120 | + variants: { |
| 121 | + backgroundColor: ['responsive', 'hover', 'focus', 'focus-visible'], |
| 122 | + }, |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +Browser support is still pretty weak on this but getting better. In the mean time, check out the [polyfill](https://github.com/WICG/focus-visible) and corresponding [PostCSS plugin](https://github.com/csstools/postcss-focus-visible) if you'd like to use this in all browsers right away. |
| 127 | + |
| 128 | +### New `checked` variant |
| 129 | + |
| 130 | +We've added a new `checked` variant you can use to conditionally style things like checkboxes and radio buttons: |
| 131 | + |
| 132 | +```html |
| 133 | +<input type="checkbox" class="bg-white checked:bg-blue-500" /> |
| 134 | +``` |
| 135 | + |
| 136 | +It's not enabled for anything by default, but you can enable it in the `variants` section of your config file: |
| 137 | + |
| 138 | +```js |
| 139 | +// tailwind.config.js |
| 140 | +module.exports = { |
| 141 | + // ... |
| 142 | + variants: { |
| 143 | + backgroundColor: ['responsive', 'hover', 'focus', 'checked'], |
| 144 | + }, |
| 145 | +} |
| 146 | +``` |
0 commit comments