All Projects → featurematrix → React Scoped Styles

featurematrix / React Scoped Styles

Scoped Styles for React

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Scoped Styles

leeks.js
Simple ANSI styling for your terminal
Stars: ✭ 12 (-73.33%)
Mutual labels:  styles
draper
🍸 React Native style utilities
Stars: ✭ 24 (-46.67%)
Mutual labels:  styles
Styled Components Theme
Defines themes via flexible color selectors for use with styled-components
Stars: ✭ 302 (+571.11%)
Mutual labels:  styles
log-utils
Basic logging utils: colors, symbols and timestamp.
Stars: ✭ 24 (-46.67%)
Mutual labels:  styles
WPFControls-ThemePack
Custom designed themes for WPF controls to make your app look better. Simple to modify.
Stars: ✭ 28 (-37.78%)
Mutual labels:  styles
ModernWpf
Modern styles and controls for your WPF applications without need WinRT
Stars: ✭ 65 (+44.44%)
Mutual labels:  styles
CustomWebCheckbox
An example of a make checkbox design on the web.
Stars: ✭ 12 (-73.33%)
Mutual labels:  styles
Ngx Ui
🚀 Style and Component Library for Angular
Stars: ✭ 534 (+1086.67%)
Mutual labels:  styles
highcharts-themes-collection
Highcharts themes collection
Stars: ✭ 30 (-33.33%)
Mutual labels:  styles
Xui
💍A simple and elegant Android native UI framework, free your hands! (一个简洁而优雅的Android原生UI框架,解放你的双手!)
Stars: ✭ 3,571 (+7835.56%)
Mutual labels:  styles
chameleon
CSS like framework for Android
Stars: ✭ 56 (+24.44%)
Mutual labels:  styles
postcss-gtk
Processes GTK+ CSS into browser CSS
Stars: ✭ 23 (-48.89%)
Mutual labels:  styles
create-class-names
A utility to extend the values of a classNames object.
Stars: ✭ 16 (-64.44%)
Mutual labels:  styles
stylenames
A simple JavaScript utility for conditionally joining inline styles together
Stars: ✭ 18 (-60%)
Mutual labels:  styles
Git Cop
DEPRECATED: Use Git Lint (https://www.alchemists.io/projects/git-lint) instead.
Stars: ✭ 352 (+682.22%)
Mutual labels:  styles
jss-material-ui
A enhanced styling engine for material-ui
Stars: ✭ 15 (-66.67%)
Mutual labels:  styles
docz-plugin-css
This package makes it possible to use preprocessors and css modules on docz
Stars: ✭ 41 (-8.89%)
Mutual labels:  styles
Graphql Css
A blazing fast CSS-in-GQL™ library.
Stars: ✭ 672 (+1393.33%)
Mutual labels:  styles
Stylist
A stylist creates cool styles. Stylist is a Gradle plugin that codegens a base set of Android XML themes.
Stars: ✭ 354 (+686.67%)
Mutual labels:  styles
Merge Duplicate Symbols
Sketch plugin to merge symbols and layer&text styles.
Stars: ✭ 272 (+504.44%)
Mutual labels:  styles

Scoped Styles for React

NPM

Get your CSS classes scoped by component directory

How it's different from CSS Modules?

In CSS Modules you have to manually import and assign classes

import styles from './button.styl';

const Button = () => (
  <button className={styles.foo}>Press Me</button>
);

React Scoped Styles doesn't require to change the conventional styling workflow. You still assign your classes with plain strings.

import './button.styl';

const Button = () => (
  <button className="foo">Press Me</button>
);

Installation

npm i react-scoped-styles

Usage

The module assumes that component file and its styles are in the same directory. This is sample for Stylus. The same applies for Sass and others.

+-- button
   +-- Button.tsx
   +-- button.styl

Button.tsx

  import React from 'react';
  import './button.styl';

  const Button = () => (
    <button className="foo">Press Me</button>
  );

  export default Button;

button.styl

.foo
    border none
    padding 10px 30px
    color white
    background-color darkslateblue

This will be rendered to

<button class="button-c65bae6565-foo">Press Me</button>
.button-c65bae6565-foo {
  border: none;
  padding: 10px 30px;
  color: #fff;
  background-color: #483d8b;
}

Getting started

The module exposes two loaders both for componenets and styles.
Append the script-loader after it has been transpiled by TypeScript or Babel.
And style-loader should be after the preprocessor loader and before the css-loader.

webpack.config.js

module.exports = {
  module: {
    rules: [
      // TypeScript
      {
        test: /\.tsx?$/,
        use: [
          'react-scoped-styles/script-loader',
          'awesome-typescript-loader'
        ]
      },
      // Babel
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: [
          'react-scoped-styles/script-loader',
          {
            loader: 'babel-loader',
            options: {
              presets: ['@babel/preset-env', '@babel/preset-react']
            }
          }
        ]
      },
      // Stylus
      {
        test: /\.styl$/,
        use: [
          'style-loader',
          'css-loader',
          'react-scoped-styles/style-loader',
          'stylus-loader'
        ]
      },
      // Sass
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'react-scoped-styles/style-loader',
          'sass-loader'
        ]
      }
    ]
  }
};

Globals

To use global styles you can pass globalsPrefix options to both loaders and prefix your classes with it.
(app is applied by default)

const scopedStylesOptions = {
  globalsPrefix: 'app'
};


{
  loader: 'react-scoped-styles/script-loader',
  options: scopedStylesOptions
}
// ...
{
  loader: 'react-scoped-styles/style-loader',
  options: scopedStylesOptions
}

Thus classes with app- prefix will be ignored.

const Button = () => (
    <button className="foo app-global-class">Press Me</button>
);
.foo
    border none
    padding 10px 30px
    color white
    background-color darkslateblue

.app-global-class
    background-color purple

Becomes

<button class="button-c65bae6565-foo app-global-class">Press Me</button>
.button-c65bae6565-foo {
  border: none;
  padding: 10px 30px;
  color: #fff;
  background-color: #483d8b;
}
.app-global-class {
  background-color: #800080;
}

Conditional classes

To use conditional classnames you can use the classes function.
Note that the classnames should be inline

import React, { useState } from 'react';
import { classes } from 'react-scoped-styles';
import './sidebar.styl';

export const SideBar = () => {
    const [open, setOpen] = useState(false);

    return (
        <div className={classes([open, 'open'], 'sidebar')}>
            ...
        </div>
    )
};

API

classes (
    ...([boolean, string] | string)[]
) => string;

classes function accepts arrays of [condition, className] pairs, and class-name strings or default classes.

<div className={classes('default', [true, 'applied'], 'another-one', [false, 'not-applied'])} />

All classes should be INLINE, this won't work

const someClass = 'some';
const someCondition = true;

<div className={classes([someCondition, someClass])} />
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].