Allows to copy-paste tailwind examples. This feature can be used together with your favorite framework without any additional setup.
The twind/observe modules allows to use the class attribute for tailwind rules. If such a rule is detected the corresponding CSS rule is created and injected into the stylesheet. No need for tw but it can be used on the same elements as well.
This is meant for advanced use cases. Most of the time you may want to use twind/shim.
twind/observe uses the default/global tw instance if not configured otherwise. You can provide a custom instance in several ways:
import { create } from'twind'import { observe, createObserver } from'twind/observe'// Create a custom instanceconstinstance = create(/* ... */)// 1. As second parameterobserve(document.body, instance)// 2. As this contextobserve.call(instance, document.body)observe.bind(instance)(document.body)// 3. Use the factorycreateObserver(instance).observe(document.body)
import { createObserver, observe } from'twind/observe'constobserver = createObserver(/* custom instance */)// Or to start observing an element right away// const observer = observe(node, /* custom instance */)// Start observing a node; can be called several times with different nodesobserver.observe(node)// Stop observing all nodesobserver.disconnect()
import { LitElement, html } from'lit-element'import { create, cssomSheet } from'twind'import { createObserver } from'twind/observe'// 1. Create separate CSSStyleSheetconstsheet = cssomSheet({ target:newCSSStyleSheet() })// 2. Use that to create an own twind instanceconstinstance = create({ sheet })classTwindElementextendsLitElement {// 3. Apply the same style to each instance of this componentstaticstyles = [sheet.target]// 4. Start observing class attributes changesconnectedCallback() {super.connectedCallback()this._observer = createObserver(instance).observe(this.renderRoot) }// 5. Stop observing class attributes changesdisconnectedCallback() {super.disconnectedCallback()this._observer.disconnect() }render() {// 5. Use tailwind rules in class attributesreturnhtml` <main class="h-screen bg-purple-400 flex items-center justify-center"> <h1 class="font-bold text(center 5xl white sm:gray-800 md:pink-700)">This is Twind!</h1> </main> ` }}customElements.define('twind-element', TwindElement)document.body.innerHTML = '<twind-element></twind-element>'
This uses a MutationObserver to detect changed class attributes or added DOM nodes. On detection the class attribute is parsed and translated by twind to inject the required classes into the stylesheet and the class attribute is updated to reflect the added CSS class names that may have been hashed.
The
twind/observe
modules allows to use theclass
attribute for tailwind rules. If such a rule is detected the corresponding CSS rule is created and injected into the stylesheet. No need fortw
but it can be used on the same elements as well.Usage
All Twind syntax features like grouping are supported within class attributes
Customization
twind/observe
uses the default/globaltw
instance if not configured otherwise. You can provide a custom instance in several ways:API
Example
This example shows how a custom observer instance can be used to shim a web component.
Implementation Details
This uses a MutationObserver to detect changed class attributes or added DOM nodes. On detection the class attribute is parsed and translated by twind to inject the required classes into the stylesheet and the class attribute is updated to reflect the added CSS class names that may have been hashed.