GithubHelp home page GithubHelp logo

streamich / styletron Goto Github PK

View Code? Open in Web Editor NEW

This project forked from styletron/styletron

0.0 2.0 0.0 828 KB

:zap: Universal, high-performance JavaScript styles

Home Page: http://styletron.js.org

License: MIT License

JavaScript 95.16% TypeScript 4.62% CSS 0.22%

styletron's Introduction

Styletron logo

build status dependencies status npm version

Universal, high-performance JavaScript styles.

What is Styletron?

Styletron is a universal CSS-in-JS engine built from the ground up for high-performance. Features include:

Advanced critical rendering path optimization of server-rendered pages

  • Dynamic generation of inlineable critical stylesheets with minimum possible size and parse times
    • Automatic generation of maximally compressed "atomic" critical CSS via declaration-level deduplication
    • Automatic declaration-level dead CSS elimination - only actually used declarations get included in output
  • Native media query and pseudo selector support for full critical CSS without JavaScript

Efficient dynamic client-side styles

  • Hyper-granular memoization to avoid making unnecessary modifications to stylesheet
  • Fast cache hydration of server-rendered styles to prevent re-rendering of server-rendered styles
  • Use of CSSStyleSheet rule injection ensuring only new styles get parsed

Check out the introductory blog post to learn more

For framework and library authors

The core Styletron module is a small, generic utility that is entirely independent of React so it can be integrated into virtually any web app. Additionally, many CSS-in-JS interfaces can be implemented with Styletron as a result of its low-level, unopinionated API.

Core API overview

import Styletron from 'styletron'; // either styletron-server or styletron-client (package.json browser field)
const styletron = new Styletron();
styletron.injectDeclaration({prop: 'color', val: 'red'});
// → 'a'
styletron.injectDeclaration({prop: 'color', val: 'red', media: '(min-width: 800px)'});
// → 'b'
styletron.injectDeclaration({prop: 'color', val: 'blue'});
// → 'c'

Injecting style objects

The styletron-utils packages includes some convenient helper functions that make working with the core API easier.

import {injectStyle} from 'styletron-utils';
injectStyle(styletron, {
  color: 'red',
  display: 'inline-block'
});
// → 'a d'
injectStyle(styletron, {
  color: 'red',
  fontSize: '1.6em'
});
// → 'a e'

Pseudo classes and media queries

The object literal syntax supported by styletron-utils also supports pseudo classes and media queries. This syntax is also supported in the styletron-react package.

injectStyle(styletron, {
  fontSize: '36px',
  '@media (max-width: 768px)': {
    fontSize: '24px'
  },
  ':hover': {
    backgroundColor: 'papayawhip'
  }
});

Full API documentation for Styletron is available at http://styletron.js.org

Using Styletron with React

Live Demo

The styletron-react package provides a convenient way to use Styletron in React applications. The API is inspired by the wonderful styled-components library, except with style objects instead of template strings.

Note: this is just one high-level interface, many others are possible with Styletron.

Creating styled element components

Static styles

import {styled} from 'styletron-react';

const Panel = styled('div', {
  backgroundColor: 'lightblue',
  fontSize: '12px'
});

<Panel>Hello World</Panel>

Using props and context in styles

import {styled} from 'styletron-react';

const Panel = styled('div', (props) => ({
  backgroundColor: props.alert ? 'orange' : 'lightblue',
  fontSize: '12px'
}));

<Panel alert>Danger!</Panel>

Extending other styled element components

import {styled} from 'styletron-react';

const DeluxePanel = styled(Panel, (props) => ({
  backgroundColor: props.alert ? 'firebrick' : 'rebeccapurple',
  color: 'white',
  boxShadow: '3px 3px 3px darkgray'
}));

<DeluxePanel>Bonjour Monde</DeluxePanel>

Styling third party components

import {styled} from 'styletron-react';

// third party components must consume a `className` prop
const ThirdParty = ({className, href, children}) =>
  <a className={className} href={href}>{children}</a>

const StyledThirdParty = styled(ThirdParty, (props) => ({
  color: 'gold'
}));

<StyledThirdParty href="/foo">Foo</StyledThirdParty>

App integration and server-side rendering

Server-side rendering

import Styletron from 'styletron-server';
import {StyletronProvider} from 'styletron-react';

function render() {
  const styletron = new Styletron();
  const appMarkup = ReactDOM.renderToString(
    <StyletronProvider styletron={styletron}>
      <App/>
    </StyletronProvider>
  );

  const stylesForHead = styletron.getStylesheetsHtml();
  
  return `<html><head>${stylesForHead}</head><body>${appMarkup}</body></html>`;
}

Client-side rendering w/ hydration

import Styletron from 'styletron-client';
import {StyletronProvider} from 'styletron-react';

const styleElements = document.getElementsByClassName('_styletron_hydrate_');

ReactDOM.render(
  <StyletronProvider styletron={new Styletron(styleElements)}>
    <App/>
  </StyletronProvider>,
  document.getElementById('app')
);

Client-side rendering only

import Styletron from 'styletron-client';
import {StyletronProvider} from 'styletron-react';

ReactDOM.render(
  <StyletronProvider styletron={new Styletron()}>
    <App/>
  </StyletronProvider>,
  document.getElementById('app')
);

styletron's People

Contributors

cjpatoilo avatar frenic avatar gandem avatar greenkeeper[bot] avatar jbellsey avatar kokjinsam avatar mmedal avatar rtsao avatar scott113341 avatar sergiodxa avatar stevenbenisek avatar wagerfield avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.