Skip to content

Commit e06a483

Browse files
committed
add Lit example
1 parent e71dee0 commit e06a483

File tree

19 files changed

+343
-303
lines changed

19 files changed

+343
-303
lines changed

.changeset/nasty-meals-fail.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@example/with-lit': patch
3+
'@twind/core': patch
4+
'@sites/twind.style': patch
5+
---
6+
7+
add Lit example

documentation/with-gatsby.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package: 'gatsby-plugin-twind'
55
example: 'with-gatsby'
66
excerpt: |
77
Setting up Twind for seamless integration in a [Gatsby](https://gatsbyjs.com) project.
8-
next: ./with-next.md
8+
next: ./with-lit.md
99
---
1010

1111
## 🤝 Compatibility

documentation/with-lit.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
section: Use With
3+
title: Lit
4+
example: with-lit
5+
excerpt: |
6+
Using Twind with [Lit](https://lit.dev)
7+
next: ./with-next.md
8+
---
9+
10+
This guide shows how to use [Lit](https://lit.dev) with Twind.
11+
12+
> **Caution**
13+
> This example is using [Constructable Stylesheet Objects](https://wicg.github.io/construct-stylesheets/) and `DocumentOrShadowRoot.adoptedStyleSheets` which have [limited browser support](https://caniuse.com/mdn-api_document_adoptedstylesheets) at the moment (December 2022). The [Constructible style sheets polyfill](https://github.com/calebdwilliams/construct-style-sheets) offers a solution for all modern browsers and IE 11.
14+
15+
```js
16+
import { LitElement, html } from 'lit'
17+
18+
import { twind, cssom, observe } from '@twind/core'
19+
import config from './twind.config'
20+
21+
// 1. Create separate CSSStyleSheet
22+
const sheet = cssom(new CSSStyleSheet())
23+
24+
// 2. Use that to create an own twind instance
25+
const tw = twind(config, sheet)
26+
27+
export class TwindElement extends LitElement {
28+
// 3. Apply the same style to each instance of this element
29+
static override styles = [sheet.target]
30+
31+
// 4a. Observe using "own" tw function
32+
override firstUpdated(): void {
33+
observe(tw, this.renderRoot)
34+
}
35+
36+
override render() {
37+
return html`
38+
<main class="h-screen bg-purple-400 flex items-center justify-center">
39+
<h1 class="font-bold text(center 5xl white sm:gray-800 md:pink-700)">This is Twind!</h1>
40+
</main>
41+
`
42+
43+
// 4b. Use "own" tw function directly
44+
// return html`
45+
// <main class="${tw('h-screen bg-purple-400 flex items-center justify-center')}">
46+
// <h1 class="${tw('font-bold text(center 5xl white sm:gray-800 md:pink-700')}">
47+
// This is Twind!
48+
// </h1>
49+
// </main>
50+
// `
51+
}
52+
}
53+
54+
customElements.define('twind-element', TwindElement);
55+
```
56+
57+
> **Tip**
58+
> The [Library Mode guide](./library-mode) might be helpful for advanced use cases like creating a dedicated `twind` module.
59+
60+
```js title="twind.js"
61+
import {
62+
twind,
63+
cssom,
64+
tx as tx$,
65+
injectGlobal as injectGlobal$,
66+
keyframes as keyframes$,
67+
} from '@twind/core'
68+
69+
import config from './twind.config'
70+
71+
// 1. Create separate CSSStyleSheet
72+
export const sheet = /* #__PURE__ */ cssom(new CSSStyleSheet())
73+
74+
// 2. Use that to create an own twind instance
75+
export const tw = /* #__PURE__ */ twind(config, sheet)
76+
77+
// tx allow tagged template usage like: tx`text(center 5xl white sm:gray-800 md:pink-700)`
78+
export const tx = /* #__PURE__ */ tx$.bind(tw)
79+
80+
// bind some helper function
81+
export const injectGlobal = /* #__PURE__ */ injectGlobal$.bind(tw)
82+
export const keyframes = /* #__PURE__ */ keyframes$.bind(tw)
83+
84+
// export additional useful functions
85+
export { observe, cx } from from '@twind/core'
86+
```

documentation/with-web-components.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TwindElement extends HTMLElement {
2828

2929
const shadow = this.attachShadow({ mode: 'open' })
3030

31-
// 3. Apply the same style to each instance of this component
31+
// 3. Apply the same style to each instance of this element
3232
shadow.adoptedStyleSheets = [sheet.target]
3333

3434
// 4a. Observe using "own" tw function
@@ -44,8 +44,8 @@ class TwindElement extends HTMLElement {
4444

4545
// 4b. Use "own" tw function directly
4646
// shadow.innerHTML = `
47-
// <main class="${tw`h-screen bg-purple-400 flex items-center justify-center`}">
48-
// <h1 class="${tw`font-bold text(center 5xl white sm:gray-800 md:pink-700)`}">
47+
// <main class="${tw('h-screen bg-purple-400 flex items-center justify-center')}">
48+
// <h1 class="${tw('font-bold text(center 5xl white sm:gray-800 md:pink-700)')}">
4949
// This is Twind!
5050
// </h1>
5151
// </main>
@@ -57,3 +57,34 @@ customElements.define('twind-element', TwindElement)
5757

5858
document.body.innerHTML = '<twind-element></twind-element>'
5959
```
60+
61+
> **Tip**
62+
> The [Library Mode guide](./library-mode) might be helpful for advanced use cases like creating a dedicated `twind` module.
63+
64+
```js title="twind.js"
65+
import {
66+
twind,
67+
cssom,
68+
tx as tx$,
69+
injectGlobal as injectGlobal$,
70+
keyframes as keyframes$,
71+
} from '@twind/core'
72+
73+
import config from './twind.config'
74+
75+
// 1. Create separate CSSStyleSheet
76+
export const sheet = /* #__PURE__ */ cssom(new CSSStyleSheet())
77+
78+
// 2. Use that to create an own twind instance
79+
export const tw = /* #__PURE__ */ twind(config, sheet)
80+
81+
// tx allow tagged template usage like: tx`text(center 5xl white sm:gray-800 md:pink-700)`
82+
export const tx = /* #__PURE__ */ tx$.bind(tw)
83+
84+
// bind some helper function
85+
export const injectGlobal = /* #__PURE__ */ injectGlobal$.bind(tw)
86+
export const keyframes = /* #__PURE__ */ keyframes$.bind(tw)
87+
88+
// export additional useful functions
89+
export { observe, cx } from from '@twind/core'
90+
```

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Some examples of how to use the Twind. These are great for reproductions of issu
2020
| Example | Try it live at | Description |
2121
| -------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
2222
| [Gatsby](https://github.com/tw-in-js/twind/tree/main/examples/gatsby) | ~~[Stackblitz](https://stackblitz.com/fork/github/tw-in-js/twind/tree/main/examples/with-gatsby)~~[Codesandbox](https://githubbox.com/tw-in-js/twind/tree/main/examples/with-gatsby) | with [Gatsby](https://www.gatsbyjs.com) using [gatsby-plugin-twind](https://github.com/tw-in-js/twind/tree/main/packages/with-gatsby), [@twind/preset-autoprefix](https://github.com/tw-in-js/twind/tree/main/packages/preset-autoprefix) and [@twind/preset-tailwind](https://github.com/tw-in-js/twind/tree/main/packages/preset-tailwind) |
23+
| [Lit](https://github.com/tw-in-js/twind/tree/main/examples/with-lit) | [Stackblitz](https://stackblitz.com/fork/github/tw-in-js/twind/tree/main/examples/with-lit)[Codesandbox](https://githubbox.com/tw-in-js/twind/tree/main/examples/with-lit) | with [Lit](https://lit.dev) using [@twind/preset-autoprefix](https://github.com/tw-in-js/twind/tree/main/packages/preset-autoprefix) and [@twind/preset-tailwind](https://github.com/tw-in-js/twind/tree/main/packages/preset-tailwind) |
2324
| [Next.js](https://github.com/tw-in-js/twind/tree/main/examples/with-next) | [Stackblitz](https://stackblitz.com/fork/github/tw-in-js/twind/tree/main/examples/with-next)[Codesandbox](https://githubbox.com/tw-in-js/twind/tree/main/examples/with-next) | with [Next.js](https://nextjs.org) using [@twind/with-next](https://github.com/tw-in-js/twind/tree/main/packages/with-next), [@twind/preset-autoprefix](https://github.com/tw-in-js/twind/tree/main/packages/preset-autoprefix) and [@twind/preset-tailwind](https://github.com/tw-in-js/twind/tree/main/packages/preset-tailwind) |
2425
| [Remix](https://github.com/tw-in-js/twind/tree/main/examples/with-remix) | [Stackblitz](https://stackblitz.com/fork/github/tw-in-js/twind/tree/main/examples/with-remix)[Codesandbox](https://githubbox.com/tw-in-js/twind/tree/main/examples/with-remix) | with [Remix](https://remix.run) using [@twind/with-remix](https://github.com/tw-in-js/twind/tree/main/packages/with-remix), [@twind/preset-autoprefix](https://github.com/tw-in-js/twind/tree/main/packages/preset-autoprefix) and [@twind/preset-tailwind](https://github.com/tw-in-js/twind/tree/main/packages/preset-tailwind) |
2526
| [SvelteKit](https://github.com/tw-in-js/twind/tree/main/examples/with-sveltekit) | [Stackblitz](https://stackblitz.com/fork/github/tw-in-js/twind/tree/main/examples/with-sveltekit)[Codesandbox](https://githubbox.com/tw-in-js/twind/tree/main/examples/with-sveltekit) | with [SvelteKit](https://kit.svelte.dev) using [@twind/with-sveltekit](https://github.com/tw-in-js/twind/tree/main/packages/with-sveltekit), [@twind/preset-autoprefix](https://github.com/tw-in-js/twind/tree/main/packages/preset-autoprefix) and [@twind/preset-tailwind](https://github.com/tw-in-js/twind/tree/main/packages/preset-tailwind) |

examples/with-lit/.stackblitzrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"installDependencies": true,
3+
"startCommand": "npm start"
4+
}

examples/with-lit/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Example: Lit
2+
3+
> Try it live at [Stackblitz](https://stackblitz.com/fork/github/tw-in-js/twind/tree/main/examples/with-lit) or [Codesandbox](https://githubbox.com/tw-in-js/twind/tree/main/examples/with-lit).
4+
5+
This example uses
6+
7+
- [lit](https://lit.dev)
8+
- [@twind/core](https://github.com/tw-in-js/twind/tree/main/packages/core)
9+
- [@twind/preset-autoprefix](https://github.com/tw-in-js/twind/tree/main/packages/preset-autoprefix)
10+
- [@twind/preset-tailwind](https://github.com/tw-in-js/twind/tree/main/packages/preset-tailwind)

examples/with-lit/favicon.ico

33.7 KB
Binary file not shown.

examples/with-lit/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" href="/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Using Lit with Twind</title>
8+
<script type="module" src="/src/index.ts"></script>
9+
</head>
10+
<body style="margin: 0">
11+
<twind-element></twind-element>
12+
</body>
13+
</html>

examples/with-lit/package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@example/with-lit",
3+
"description": "Lit example",
4+
"version": "1.0.3",
5+
"private": true,
6+
"main": "index.js",
7+
"license": "MIT",
8+
"bugs": "https://github.com/tw-in-js/twind/issues",
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/tw-in-js/twind.git",
12+
"directory": "examples/with-lit"
13+
},
14+
"engines": {
15+
"node": ">=14"
16+
},
17+
"type": "module",
18+
"scripts": {
19+
"start": "vite",
20+
"build": "vite build",
21+
"preview": "vite preview"
22+
},
23+
"dependencies": {
24+
"@twind/core": "1.0.3",
25+
"@twind/preset-autoprefix": "1.0.3",
26+
"@twind/preset-tailwind": "1.0.3",
27+
"lit": "^2.5.0"
28+
},
29+
"devDependencies": {
30+
"typescript": "~4.8.4",
31+
"vite": "^4.0.1"
32+
}
33+
}

0 commit comments

Comments
 (0)