Giter VIP home page Giter VIP logo

decoupled-styling-css-modules's Introduction

Decoupled styling

Solution - React + TypeScript + CSS-Modules

Stack

Goals

Mission statement:

Decoupling from styling for UI-components should be on similar level as in native HTML/CSS (HTML element is decoupled from its styling by exposing an API to inject styling via class attribute or className property).

Requirements:

  1. UI component is decoupled from its styling. Styling is injected into the component via its API.
  2. UI component has its default, minimal styling, in case no styling is injected.
  3. Default styling may be overwritten by custom styling via styling-API.
  4. Default styling absent from the styling-API, can be also overwritten (although "unsafely", with regards to SemVer).
  5. Styling can be set for the UI-component and its children (and grand-children etc.)
  6. It's possible to customise the styling for the whole application in one run (styling theme).

Additional requirements (no steps backwards):

  1. UI components styles are isolated (no name-conflicts, styles don't leak-in and out)
  2. No implicit dependencies to styles
  3. UI components should be decoupled from each other, as far as possible

Solution

Examples of usage:

To simplify examples, global CSS-classes are used.

  1. Native element with default styling:
<textarea />
  1. UI-Component with default styling:
<MyButton />
  1. Native element with custom styling (pass a CSS-class):
<textarea className="center" />
  1. UI-Component with custom styling (pass an object containing CSS-classes):
<MyButton classes={rootPosition: "center", content: "fixed-width"} />

It's up to the component itself to expose its styling-API.

  1. Higher-level component (nested objects containing CSS-classes to overwrite children styles):
<App classes={title: "big", okButton: {rootPosition: "left"}} />

Demo

MyButton component

Higher-level - App component

Application-level - themes

Customisation

  • To make nested component customisation possible with CSS Modules, the nested() function is used in components to construct nested-object styling-API from flat CSS files. CSS Modules don't support nested component's customization yet - see issue.
  • Overwriting component's default styles is done "per class" (instead of "per CSS-property" like natively in CSS). In case there is need to alter just one CSS-property, it's possible to use composition like here in app component.
  • native CSS overwriting solution by CSS Cascade is not used (unless composition is used). CSS-classes are not combined, but simply replaced if customised.
  • when composition is applied, CSS Modules does not copy the styles but concatenates selectors to extend styles

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.