Socket
Socket
Sign inDemoInstall

twind

Package Overview
Dependencies
3
Maintainers
2
Versions
159
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

twind


Version published
Maintainers
2
Created

Readme

Source

twind MIT License Latest Release Github


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 or more of the existing presets:

Here are some examples of how to write your own rules or variants:

Additionally we provides several integrations:

To get you started, take a look at the examples.

Usage

npm install twind@next
import { setup } from 'twind'

// You must call setup at least once, but can call it multiple times
setup({
  /* options */
})

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({
      /* options */
    })
  </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'

// can be called as many times as you want.
const tw = setup({
  /* config */
})
// -> tw === import { tw } from 'twind'

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'

// Set a className
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

extract(html, tw)

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'

// can be in a different file — but should be called at least once
setup({
  /* config */
})

function render() {
  const { html, css } = extract(renderApp(), tw)

  // inject as last element into the head
  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)

  1. parse the markup and process element classes with the provided Twind instance
  2. update the class attributes if necessary
  3. return the HTML string with the final element classes
import { setup, tw, consume, stringify } from 'twind'

// can be in a different file — but should be called at least once
setup({
  /* config */
})

function render() {
  const html = renderApp()

  // clear all styles
  tw.clear()

  // generated markup
  const markup = comsume(html, tw)

  // create CSS
  const css = stringify(tw.target)

  // inject as last element into the head
  return markup.replace('</head>', `<style data-twind>${css}</style></head>`)
}

Keywords

FAQs

Last updated on 05 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc