twind
READ THIS FIRST!
Twind v1 is still in beta. Expect bugs!
Utility-first CSS without any build step right in the browser or any other environment like Node.js, deno, workers, ...
Twind does not include any core utilities — use one of the existing presets:
Or write your own variants and rules.
Take a look at examples.
Additionally we provides several integrations:
Usage
npm install twind@next
import { setup } from 'twind'
setup({
})
Incase you are not using SSR to inject the pre-computed styles apply the following pattern to prevent FOUC:
<body class="!block" style="display: none">
</body>
If any element has the autofocus
attribute, Twind will focus it after all styles are injected.
Usage with a script tag
Add this line to your index.html
:
<head>
<script src="https://cdn.jsdelivr.net/npm/twind@next" crossorigin></script>
<script>
twind.setup({
})
</script>
</head>
To add presets add their ids to the script src
attribute:
<head>
<script
src="https://cdn.jsdelivr.net/npm/twind@next,npm/@twind/preset-tailwind@next"
crossorigin
></script>
<script>
twind.setup({
presets: [twind.presetTailwind()],
})
</script>
</head>
API
If you are using the script
tag these methods are available via the twind
global object (eg twind.setup
).
setup(config [, sheet [, target]])
Initializes Twind and can be called as many times as you want.
import { setup } from 'twind'
const tw = setup({
})
tw(...tokens)
— the current Twind instance
import { tw } from 'twind'
tw`underline`
tw({ underline: true })
tw.theme('colors.blue.500', 'blue')
cx
import { cx } from 'twind'
element.className = cx`
underline
/* multi
line
comment
*/
hover:focus:!{
sm:{italic why}
lg:-{px}
-mx-1
}
// Position
!top-1 !-bottom-2
text-{xl black}
`
shortcut
TDB
css
TDB
style
TDB
Used for static HTML processing (usually to provide SSR support for your javascript-powered web apps) — powered by consume(html, tw)
Note: Consider using inject instead.
Note: This clears the Twind instance before processing the HTML.
import { setup, extract, tw } from 'twind'
setup({
})
function render() {
const { html, css } = extract(renderApp(), tw)
return html.replace('</head>', `<style data-twind>${css}</style></head>`)
}
Low-Level API
twind
TDB
consume(html, tw)
Used for static HTML processing (usually to provide SSR support for your javascript-powered web apps)
- parse the markup and process element classes with the provided Twind instance
- update the class attributes if necessary
- return the HTML string with the final element classes
import { setup, tw, consume, stringify } from 'twind'
setup({
})
function render() {
const html = renderApp()
tw.clear()
const markup = comsume(html, tw)
const css = stringify(tw.target)
return markup.replace('</head>', `<style data-twind>${css}</style></head>`)
}