All Projects → lukejacksonn → Oceanwind

lukejacksonn / Oceanwind

Compiles tailwind shorthand into css at runtime

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Oceanwind

Vue Emotion
Seamlessly use emotion (CSS-in-JS) with Vue.js
Stars: ✭ 173 (-30.24%)
Mutual labels:  css-in-js
Nano Component
Fast & simple React component styles in under 1kb
Stars: ✭ 202 (-18.55%)
Mutual labels:  css-in-js
Styled Ppx
Typed styled components in Reason, OCaml and ReScript
Stars: ✭ 236 (-4.84%)
Mutual labels:  css-in-js
Framework
🎨 Aesthetic is an end-to-end multi-platform styling framework that offers a strict design system, robust atomic CSS-in-JS engine, a structural style sheet specification (SSS), a low-runtime solution, and much more!
Stars: ✭ 176 (-29.03%)
Mutual labels:  css-in-js
Emotion
👩‍🎤 CSS-in-JS library designed for high performance style composition
Stars: ✭ 14,177 (+5616.53%)
Mutual labels:  css-in-js
Axs
Stupid simple style components for React
Stars: ✭ 214 (-13.71%)
Mutual labels:  css-in-js
Jsxstyle
Inline style system for React and Preact
Stars: ✭ 1,990 (+702.42%)
Mutual labels:  css-in-js
Styled Props
Simple lib that allows you to set styled props in your styled-components without stress.
Stars: ✭ 247 (-0.4%)
Mutual labels:  css-in-js
Css In Js
Autocomplete React Native / JS Styles and converting plain CSS to JS styles
Stars: ✭ 192 (-22.58%)
Mutual labels:  css-in-js
Styled Email Components
💌 styled-components for emails
Stars: ✭ 231 (-6.85%)
Mutual labels:  css-in-js
Grid
This package has moved and renamed
Stars: ✭ 2,079 (+738.31%)
Mutual labels:  css-in-js
Styled Theme
Extensible theming system for styled-components 💅
Stars: ✭ 183 (-26.21%)
Mutual labels:  css-in-js
Style Sheet
⚡️💨 Fast styles in JavaScript with support for static CSS extraction.
Stars: ✭ 229 (-7.66%)
Mutual labels:  css-in-js
Gatsby Theme Storefront Shopify
Create a Shopify store with Gatsby JS 🛍️ 🛒
Stars: ✭ 175 (-29.44%)
Mutual labels:  css-in-js
Awesome Styled Components
A curated list of awesome styled-components resources 💅
Stars: ✭ 2,869 (+1056.85%)
Mutual labels:  css-in-js
Fela
State-Driven Styling in JavaScript
Stars: ✭ 2,097 (+745.56%)
Mutual labels:  css-in-js
Styled Components Vs Emotion
a short doc comparing the popular CSS-in-JS libraries styled-components and emotion
Stars: ✭ 204 (-17.74%)
Mutual labels:  css-in-js
Arc
React starter kit based on Atomic Design
Stars: ✭ 2,780 (+1020.97%)
Mutual labels:  css-in-js
A Journey Toward Better Style
A Journey toward better style
Stars: ✭ 245 (-1.21%)
Mutual labels:  css-in-js
Freestyler
5th generation CSS-in-JS library
Stars: ✭ 230 (-7.26%)
Mutual labels:  css-in-js

Oceanwind

compiles tailwind like shorthand syntax into css at runtime

This library takes inspiration from Tailwind and utilizes Otion to provide means of efficiently generating atomic styles from shorthand syntax and appending them to the DOM at runtime.

Server side rendering and static extraction is also supported.

⚡️ Check out the live and interactive demo


The aim here was to create a compiler that:

  • 📖 Supports all existing Tailwind shorthand syntax outlined in the docs
  • 💡 Generates only the styles required without building or purging
  • 🗜 Is smaller than the average purged css file output from the Tailwind compiler
  • ⏱ Has desirable perf characteristics at runtime
  • ⚠️ Warns the developer when unrecognized or duplicate shorthand is used

The library currently weighs under 10kb and supports the vast majority of Tailwind directives and variants.

Usage

To use the library, first import the module then invoke the default export using tagged template syntax:

import ow from 'https://unpkg.com/oceanwind';
document.body.className = ow`h-full bg-purple-500 rotate-3 scale-95`;

Running the above code will result in the following happening:

  1. Shorthand syntax will be translated into CSS (e.g. h-screen -> { height: 100vh }).
  2. All resultant CSS will be merged into a single CSS-in-JS object
  3. Each style will be assigned a unique class and appended to a stylesheet
  4. A string is returned representing all the classes that were created

It is recommended to import the following css files which help normalize styles across browsers:

Extending the default theme

Importing and invoking oceanwind directly will cause it to use default theme for directives that require themed values (like bg-red-500 for example). To customize the theme, use the themed export instead of the default export.

import { themed } from 'https://unpkg.com/oceanwind';

const ow = themed({
  colors: {
    red: {
      500: 'hotpink',
    },
  },
});

ow`bg-red-500`; // will result in a hotpink background-color

Any custom theme provided to the themed function will be deep merged with the default theme.

Function Signature

It is possible to invoke oceanwind in a multitude of different ways. For example:

// Function call passing a string
ow('bg-red-500 rounded');

// Tag Template Literal (falsey interpolations will be omitted)
ow`bg-red-500 rounded`;
ow`bg-red-500 ${false && 'rounded'}`;

// Function call passing an array (falsey items will be omitted)
ow(['bg-red-500', 'rounded']);
ow(['bg-red-500', false && 'rounded']);

// Function call passing an object (keys with falsey values will be omitted)
ow({ 'bg-red-500': true, rounded: true });
ow({ 'bg-red-500': true, rounded: false });

Variant Grouping

Directives with the same variants can be grouped using parenthesis. Oceanwind will expand the nested directives; applying the variant to each directive in the group before translation. For example:

Notice any directives within tagged template literals can span multiple lines

ow`
  sm:hover:(
    bg-black
    text-white
  )
  md:(bg-white hover:text-black)
`;

It is possible to nest groupings too, for example:

ow`
  sm:(
    bg-black
    text-white
    hover:(bg-white text-black)
  )
`;

Two things to note here is that the outermost variant should always be a responsive variant (just like in tailwind hover:sm: is not supported) and that nesting responsive variants doesn't make sense either, for example sm:md: is not supported.

Catching Errors

By default warnings about missing or duplicate translations will be written to the console:

image

Clicking on the file path in dev tools will jump to the line in the file in the sources panel.

It is possible to make oceanwind throw an error rather just warning by opting into strict mode:

themed({ strict: true });

image

Example

Most of the time developers will be using a front end framework to render DOM elements. Oceanwind is framework agnostic but here is an example of how you might use it with preact and no build step.

⚡️ Check out the live and interactive demo

import { render, h } from 'https://unpkg.com/preact?module';

import htm from 'https://unpkg.com/htm?module';
import ow from 'https://unpkg.com/oceanwind';

const html = htm.bind(h);

render(
  html`
    <div className=${ow`h-full bg-purple-500`}>
      <h1 className=${ow`text-white font-bold`}>Hello World</h1>
    </div>
  `,
  document.body
);

Server-side rendering (SSR)

Oceanwind supports SSR through Otion. Consider the following example:

import { h } from 'preact';
import render from 'preact-render-to-string';
import htm from 'htm';
import { getStyleTag, VirtualInjector } from 'otion/server';
import { setup, themed } from 'oceanwind';

const injector = VirtualInjector();
setup({ injector });

const html = htm.bind(h);
const ow = themed({});
const style = {
  main: ow`clearfix`,
};

const app = html`<main className=${style.main}>hello oceanwind</main>`;
const appHtml = render(app);
const styleTag = getStyleTag(injector);

// Inject styleTag to your HTML now.

Oceanwind also exposes hydrate from Otion for client-side hydration. See Otion documentation for further configuration options and usage instructions.

Acknowledgements

I'd like to thank both Adam Wathan and Kristóf Poduszló for their amazing work with Tailwind and Otion respectively, which made making this library somewhat a breeze. Also Phil Pluckthun who helped me deduce the initial grammar.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].