Skip to content

Commit 2dc7b09

Browse files
committed
Add(Runtime): Lit integration: add cssRuntime decorator, update installation guide, and create metadata for Lit setup. Resolved #158
1 parent caefbc5 commit 2dc7b09

11 files changed

Lines changed: 243 additions & 17 deletions

File tree

‎examples/lit/src/my-element.ts‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { LitElement, css, html } from 'lit'
22
import { customElement, property } from 'lit/decorators.js'
33
import masterLogo from './assets/master.svg'
44
import litLogo from './assets/lit.svg'
5-
import CSSRuntime, { initCSSRuntime } from '@master/css-runtime'
5+
import { cssRuntime } from '@master/css-runtime'
6+
import type { CSSRuntime } from '@master/css-runtime'
67
import config from '../master.css'
78

89
/**
@@ -12,9 +13,10 @@ import config from '../master.css'
1213
* @csspart button - The button
1314
*/
1415
@customElement('my-element')
16+
@cssRuntime(config)
1517
export class MyElement extends LitElement {
1618

17-
cssRuntime: CSSRuntime | undefined
19+
cssRuntime?: CSSRuntime
1820

1921
/**
2022
* Copy for the read the docs hint.
@@ -28,16 +30,6 @@ export class MyElement extends LitElement {
2830
@property({ type: Number })
2931
count = 0
3032

31-
connectedCallback() {
32-
super.connectedCallback()
33-
this.cssRuntime = initCSSRuntime(config, this.shadowRoot!)
34-
}
35-
36-
disconnectedCallback() {
37-
super.disconnectedCallback()
38-
this.cssRuntime?.destroy()
39-
}
40-
4133
render() {
4234
return html`
4335
<div class="flex justify-center items-center">

‎examples/lit/tsconfig.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"skipLibCheck": true,
1313
/* Bundler mode */
14-
"moduleResolution": "node",
14+
"moduleResolution": "bundler",
1515
"resolveJsonModule": true,
1616
"isolatedModules": true,
1717
"noEmit": true,
@@ -24,4 +24,4 @@
2424
"include": [
2525
"src/**/*.ts"
2626
]
27-
}
27+
}

‎internal‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import type { Config } from '@master/css'
2+
import CSSRuntime from './core'
3+
import initCSSRuntime from './init'
4+
5+
type CSSRuntimeHostConstructor = new (...args: any[]) => HTMLElement
6+
type CSSRuntimeConfig = Config | ((host: HTMLElement) => Config | undefined)
7+
type CSSRuntimeRoot = ShadowRoot | ((host: HTMLElement) => ShadowRoot | null | undefined)
8+
9+
export interface CSSRuntimeDecoratorOptions {
10+
config?: CSSRuntimeConfig
11+
root?: CSSRuntimeRoot
12+
autoObserve?: boolean
13+
}
14+
15+
function isOptions(value: CSSRuntimeConfig | CSSRuntimeDecoratorOptions | undefined): value is CSSRuntimeDecoratorOptions {
16+
return !!value && (
17+
'config' in value ||
18+
'root' in value ||
19+
'autoObserve' in value
20+
)
21+
}
22+
23+
function resolveConfig(host: HTMLElement, config: CSSRuntimeConfig | undefined): Config | undefined {
24+
return typeof config === 'function' ? config(host) : config
25+
}
26+
27+
function resolveRoot(host: HTMLElement, root: CSSRuntimeRoot | undefined): ShadowRoot | null | undefined {
28+
return typeof root === 'function'
29+
? root(host)
30+
: root || host.shadowRoot
31+
}
32+
33+
export default function cssRuntime(config?: CSSRuntimeConfig): <T extends CSSRuntimeHostConstructor>(target: T) => T
34+
export default function cssRuntime(options?: CSSRuntimeDecoratorOptions): <T extends CSSRuntimeHostConstructor>(target: T) => T
35+
export default function cssRuntime(config?: CSSRuntimeConfig, options?: Omit<CSSRuntimeDecoratorOptions, 'config'>): <T extends CSSRuntimeHostConstructor>(target: T) => T
36+
export default function cssRuntime(
37+
configOrOptions?: CSSRuntimeConfig | CSSRuntimeDecoratorOptions,
38+
options?: Omit<CSSRuntimeDecoratorOptions, 'config'>
39+
) {
40+
const resolvedOptions = isOptions(configOrOptions)
41+
? configOrOptions
42+
: { ...options, config: configOrOptions }
43+
44+
return function <T extends CSSRuntimeHostConstructor>(target: T): T {
45+
const connectedCallback = target.prototype.connectedCallback as (() => void) | undefined
46+
const disconnectedCallback = target.prototype.disconnectedCallback as (() => void) | undefined
47+
48+
return class extends target {
49+
cssRuntime?: CSSRuntime
50+
51+
connectedCallback() {
52+
connectedCallback?.call(this)
53+
const root = resolveRoot(this, resolvedOptions.root)
54+
if (!root) {
55+
throw new Error('`@cssRuntime()` requires a shadow root. Provide `options.root` or create a shadow root before `connectedCallback()` finishes.')
56+
}
57+
this.cssRuntime = initCSSRuntime(
58+
resolveConfig(this, resolvedOptions.config),
59+
root,
60+
resolvedOptions.autoObserve
61+
)
62+
}
63+
64+
disconnectedCallback() {
65+
disconnectedCallback?.call(this)
66+
this.cssRuntime?.destroy()
67+
this.cssRuntime = undefined
68+
}
69+
}
70+
}
71+
}

‎packages/runtime/src/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export { default as CSSRuntime, default } from './core'
2+
export { default as cssRuntime } from './css-runtime'
23
export { default as initCSSRuntime } from './init'
34
export { default as RuntimeSyntaxLayer } from './syntax-layer'
45

56
export type * from './types'
6-
export * from './syntax-layer'
7+
export type { CSSRuntimeDecoratorOptions } from './css-runtime'
8+
export * from './syntax-layer'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { cssRuntime } from '../src'
2+
import type { Config } from '@master/css'
3+
import type { CSSRuntime } from '../src'
4+
5+
class RuntimeElement extends HTMLElement {
6+
cssRuntime?: CSSRuntime
7+
8+
connectedCallback() {
9+
const runtime: CSSRuntime | undefined = this.cssRuntime
10+
runtime?.refresh({})
11+
}
12+
}
13+
14+
cssRuntime()(RuntimeElement)
15+
cssRuntime({ variables: {} } satisfies Config)(RuntimeElement)
16+
cssRuntime({
17+
config: {} satisfies Config,
18+
root: (host) => host.shadowRoot,
19+
autoObserve: false
20+
})(RuntimeElement)

‎site/app/[locale]/guide/installation/(main)/integrations/content.mdx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Installation guides for popular frameworks and build tools integrated with Maste
1010
brands['astro'],
1111
brands['blazor'],
1212
brands['react'],
13+
brands['lit'],
1314
brands['vuejs'],
1415
brands['angular'],
1516
brands['vite'],
@@ -19,4 +20,4 @@ Installation guides for popular frameworks and build tools integrated with Maste
1920

2021
---
2122

22-
Don't have a preferred framework or tool yet? Try the [online playground](/play) or C&P [CDN](/guide/installation/cdn), or set up the [runtime](/reference/runtime) from scratch is by initializing it in your app’s entry point.
23+
Don't have a preferred framework or tool yet? Try the [online playground](/play) or C&P [CDN](/guide/installation/cdn), or set up the [runtime](/reference/runtime) from scratch is by initializing it in your app’s entry point.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<StepSection>
2+
<Step $row>
3+
<StepL>
4+
### <StepNum />Create a Lit project
5+
If you don't have a Lit project, create one first. It's recommended to refer to [Lit documentation](https://lit.dev/docs/).
6+
</StepL>
7+
<StepR>
8+
```bash name=Terminal
9+
npm create vite@latest my-app -- --template lit-ts
10+
cd my-app
11+
```
12+
</StepR>
13+
</Step>
14+
<Step $row>
15+
<StepL>
16+
### <StepNum />Install Master CSS
17+
Install [`@master/css-runtime`](/reference/runtime) into your project.
18+
</StepL>
19+
<StepR>
20+
```bash action=install
21+
@master/css-runtime@rc
22+
```
23+
</StepR>
24+
</Step>
25+
<Step $row>
26+
<StepL>
27+
### <StepNum />Create a configuration file
28+
Run the command to create a [master.css.js](/guide/configuration) file.
29+
</StepL>
30+
<StepR>
31+
```bash action=create
32+
@master/css@rc
33+
```
34+
</StepR>
35+
</Step>
36+
<Step $row>
37+
<StepL>
38+
### <StepNum />Set up CSS runtime engine
39+
Decorate your Lit element with `@cssRuntime()` to initialize Master CSS Runtime for the element's shadow root.
40+
</StepL>
41+
<StepR>
42+
```ts name=src/my-element.ts
43+
import { LitElement, html } from 'lit'
44+
import { customElement } from 'lit/decorators.js'
45+
import { cssRuntime } from '@master/css-runtime' // [!code highlight]
46+
import type { CSSRuntime } from '@master/css-runtime' // [!code highlight]
47+
import config from '../master.css' // [!code highlight]
48+
49+
@customElement('my-element')
50+
@cssRuntime(config) // [!code highlight]
51+
export class MyElement extends LitElement {
52+
cssRuntime?: CSSRuntime // [!code highlight]
53+
54+
render() {
55+
return html`
56+
<h1 class="italic m:12x fg:strong font:40 font:heavy">
57+
Hello World
58+
</h1>
59+
`
60+
}
61+
}
62+
```
63+
</StepR>
64+
</Step>
65+
<Step $row>
66+
<StepL>
67+
### <StepNum />Start dev server
68+
Run the command to start the Vite dev server.
69+
</StepL>
70+
<StepR>
71+
```bash name=Terminal&action=run
72+
dev
73+
```
74+
</StepR>
75+
</Step>
76+
<Step $row>
77+
<StepL>
78+
### <StepNum />Start using Master CSS
79+
Now style elements inside your Lit templates using Master CSS syntax!
80+
</StepL>
81+
<StepR>
82+
```ts name=src/my-element.ts
83+
render() {
84+
return html`
85+
<button class="inline-flex items:center gap:2x px:4x py:2x r:2x bg:blue fg:white">
86+
Hello Lit
87+
</button>
88+
`
89+
}
90+
```
91+
</StepR>
92+
</Step>
93+
<HelloWorld url="localhost:5173" />
94+
</StepSection>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import define from 'internal/utils/metadata'
2+
3+
const metadata = define({
4+
title: 'Set up Master CSS in Lit',
5+
description: 'Guide to setting up Master CSS in your Lit project.',
6+
category: 'Integrations',
7+
vercelOG: true,
8+
fileURL: import.meta.url
9+
})
10+
11+
export default metadata
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Props } from 'internal/types/Props'
2+
import create from 'internal/utils/og-image'
3+
import dictionaries from '~/site/dictionaries'
4+
import metadata from './metadata'
5+
import type { AbsoluteTemplateString } from 'next/dist/lib/metadata/types/metadata-types'
6+
7+
export const alt = (metadata.title as AbsoluteTemplateString)?.absolute || metadata.title as string
8+
export const contentType = 'image/jpg'
9+
export const runtime = 'nodejs'
10+
11+
export default (props: Props) => create({
12+
props,
13+
metadata,
14+
dictionaries,
15+
title: 'Runtime Rendering',
16+
icon: 'lit'
17+
})

0 commit comments

Comments
 (0)