|
| 1 | +--- |
| 2 | +chapter: 8 |
| 3 | +subtitle: Bringing CSS into ShadowDOM |
| 4 | +hidden: true |
| 5 | +--- |
| 6 | + |
| 7 | +Components with ShadowDOM typically want to introduce some CSS into their ShadowRoots. This is done with the use of `adoptedStyleSheets`, which can be a little cumbersome, so Catalyst provides the `@style` decorator and `css` utility function to more easily add CSS to your component. |
| 8 | + |
| 9 | +If your CSS lives in a different file, you can import the file with using the `assert { type: 'css' }` import assertion. You might need to configure your bundler tool to allow for this. If you're unfamiliar with this feature, you can [check out the web.dev article on CSS Module Scripts](https://web.dev/css-module-scripts/): |
| 10 | + |
| 11 | +```typescript |
| 12 | +import {controller, style} from '@github/catalyst' |
| 13 | +import DesignSystemCSS from './my-design-system.css' assert { type: 'css' } |
| 14 | + |
| 15 | +@controller |
| 16 | +class UserRow extends HTMLElement { |
| 17 | + @style designSystem = DesignSystemCSS |
| 18 | + |
| 19 | + connectedCallback() { |
| 20 | + this.attachShadow({ mode: 'open' }) |
| 21 | + // adoptedStyleSheets now includes our DesignSystemCSS! |
| 22 | + console.assert(this.shadowRoot.adoptedStyleSheets.includes(this.designSystem)) |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +Multiple `@style` tags are allowed, each one will be applied to the `adoptedStyleSheets` meaning you can split your CSS without worry! |
| 28 | + |
| 29 | +```typescript |
| 30 | +import {controller} from '@github/catalyst' |
| 31 | +import UtilityCSS from './my-design-system/utilities.css' assert { type: 'css' } |
| 32 | +import NormalizeCSS from './my-design-system/normalize.css' assert { type: 'css' } |
| 33 | +import UserRowCSS from './my-design-system/components/user-row.css' assert { type: 'css' } |
| 34 | + |
| 35 | +@controller |
| 36 | +class UserRow extends HTMLElement { |
| 37 | + @style utilityCSS = UtilityCSS |
| 38 | + @style normalizeCSS = NormalizeCSS |
| 39 | + @style userRowCSS = UserRowCSS |
| 40 | + |
| 41 | + connectedCallback() { |
| 42 | + this.attachShadow({ mode: 'open' }) |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### Defining CSS in JS |
| 48 | + |
| 49 | +Sometimes it can be useful to define small snippets of CSS within JavaScript itself, and so for this we have the `css` helper function which can create a `CSSStyleSheet` object on-the-fly: |
| 50 | + |
| 51 | +```typescript |
| 52 | +import {controller, style, css} from '@github/catalyst' |
| 53 | + |
| 54 | +@controller |
| 55 | +class UserRow extends HTMLElement { |
| 56 | + @style componentCSS = css`:host { display: flex }` |
| 57 | + |
| 58 | + connectedCallback() { |
| 59 | + this.attachShadow({ mode: 'open' }) |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +As always though, the best way to handle dynamic per-instance values is with CSS variables: |
| 65 | + |
| 66 | +```typescript |
| 67 | +import {controller, style, css} from '@github/catalyst' |
| 68 | + |
| 69 | +const sizeCSS = (size = 1) => css`:host { font-size: var(--font-size, ${size}em); }` |
| 70 | + |
| 71 | +@controller |
| 72 | +class UserRow extends HTMLElement { |
| 73 | + @style componentCSS = sizeCSS |
| 74 | + |
| 75 | + @attr set fontSize(n: number) { |
| 76 | + this.style.setProperty('--font-size', n) |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | +```html |
| 81 | +<user-row font-size="1">Alex</user-row> |
| 82 | +<user-row font-size="3">Riley</user-row> |
| 83 | +``` |
| 84 | + |
| 85 | +The `css` function is memoized; it will always return the same `CSSStyleSheet` object for every callsite. This allows you to "lift" it into a function that can change the CSS for all components by calling the function, which will replace the CSS inside it. |
| 86 | + |
| 87 | +```typescript |
| 88 | +import {controller, style, css} from '@github/catalyst' |
| 89 | + |
| 90 | +const sizeCSS = (size = 1) => css`:host { font-size: ${size}em; }` |
| 91 | + |
| 92 | +// Calling sizeCSS will always result in the same CSSStyleSheet object |
| 93 | +console.assert(sizeCSS(1) === sizeCSS(2)) |
| 94 | + |
| 95 | +@controller |
| 96 | +class UserRow extends HTMLElement { |
| 97 | + @style componentCSS = sizeCSS |
| 98 | + |
| 99 | + #size = 1 |
| 100 | + makeAllUsersLargerFont() { |
| 101 | + sizeCSS(this.#size++) |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
0 commit comments