0.1.40 • Published 1 year ago

@dxos/react-components v0.1.40

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

DXOS design system

The react-components package is the single source of truth for DXOS’s lower-level design system.

To get started, you’ll need to set up how you’ll build with this package, add peer dependencies, set up the provider, and set up theming.

Set up the build…

…with Vite

Using the design system requires opting-in in a few places, but by design it otherwise needs no configuration.

1. Add the Vite plugin

Add @dxos/react-components to the project’s dev dependencies, then extend the project’s Vite config (vite.config.ts) to use it, e.g.:

// ...
import { ThemePlugin } from '@dxos/react-components/plugin';
// ...
export default defineConfig({
  // ...
  plugins: [
    // ...
    ThemePlugin({content: [
        resolve(__dirname, '../node_modules/@dxos/react-components/dist/**/*.mjs'),
        './index.html', './src/**/*.{js,ts,jsx,tsx}'
      ]}),
    // ...
  ]
  // ...
});
// ...

Ensure content contains globs for your project’s own files that will use Tailwind classnames, as well as any DXOS design system packages your project uses, e.g.:

'./node_modules/@dxos/react-components/dist/**/*.mjs',
'./node_modules/@dxos/react-appkit/dist/**/*.mjs',
'./node_modules/@dxos/react-composer/dist/**/*.mjs',

2. Reference the basic stylesheet

In the file which calls React DOM’s createRoot or render, add:

import '@dxosTheme';

…with ESBuild

The ESBuild plugin is experimental and has several caveats noted below, proceed with caution.

1. Add the ESBuild plugin

Add @dxos/react-components to the project’s dev dependencies, then extend the project’s ESBuild config to use it, e.g.:

// ...
import { ThemePlugins } from '@dxos/react-components/esbuild-plugin';
// ...
void build({
  entryPoints: [/* ... */, resolve(__dirname, '../node_modules/@dxos/react-components/src/theme.css')],
  // ...
  plugins: ThemePlugins({
    outdir: resolve(__dirname, '../dist'),
    content: [
      resolve(__dirname, '../index.html'),
      resolve(__dirname, '../src/**/*.{js,ts,jsx,tsx}'),
      resolve(__dirname, '../node_modules/@dxos/react-components/dist/**/*.mjs')
      // other sources to scan
    ]
  })
});
// ...

Ensure theme.css from react-components is included as an entrypoint, and outdir passed to ThemePlugins is the same as provided to ESBuild.

2. Reference the basic stylesheet

Load the built stylesheet as appropriate for your project, e.g. simply add it to index.html:

<link rel="stylesheet" href="./dist/node_modules/@dxos/react-components/src/theme.css"/>

Note that this is not in your project’s node_modules directory, it’s in your project’s outdir.

…with your own build stack

This package exports both CJS and ESM builds of its components, though these are styled with Tailwind as the design token system. Any build stack can use the component builds in this package, you’ll just need to configure your build to handle Tailwind so that styles are applied correctly.

Follow the Tailwind Framework Guides documentation relevant to your stack to see how that’s done, but make the following modifications:

  • Use the Tailwind configuration from this package:
import tailwindcss from 'tailwindcss';
import { tailwindConfig } from '@dxos/react-components';
// ...
tailwindcss(
  tailwindConfig({
    content: [/* Wherever else Tailwind utility classes are used */],
    /* Optional params as neeeded */
  })
)
// ...
  • Instead of adding the Tailwind directives to your own CSS, use or import this package’s theme.css (react-components/dist/plugin/theme.css) which adds the Tailwind directives itself.
  • This package relies on font assets installed via npm as dependencies; if you’re seeing errors in the browser console or build logs about missing .woff2 files, ensure your build can correctly resolve the import directives used in theme.css e.g. @import '@fontsource/roboto-flex/variable-full.css'.

If you have any issues integrating this package with your build, please file an issue including details about your build stack and what result you’re getting.

Add peer dependencies

This package uses icons from phosphor-icons, but lists them as a peer dependency to avoid re-exporting that package; use your project’s package manager to add phosphor-icons as a dependency.

Set up the provider

In order for the project to render correctly, wrap your app with <ThemeProvider/>.

Set up theming

In order to display the correct theme, you need to mark <html> with the correct class from the <head> tag, otherwise it will flash the wrong theme:

<head>
  <script>
    if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
      document.documentElement.classList.add('dark')
    } else {
      document.documentElement.classList.remove('dark')
    }
  </script>
</head>

If you want to keep up with prefers-color-scheme, add:

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function(e){ if(e.matches){
  document.documentElement.classList.add('dark')
} else {
  document.documentElement.classList.remove('dark')
}})

You could combine the two with a script like this one:

function setTheme(darkMode) {
  document.documentElement.classList[darkMode ? 'add' : 'remove']('dark')
}
setTheme(window.matchMedia('(prefers-color-scheme: dark)').matches)
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function (e) {
  setTheme(e.matches)
});

You can augment this predicate checking any preferences saved in localStorage, etc.

Note that you can apply this classname selectively further down the DOM tree if needed.

0.1.40

1 year ago

0.1.38

1 year ago

0.1.39

1 year ago

0.1.30

1 year ago

0.1.31

1 year ago

0.1.32

1 year ago

0.1.33

1 year ago

0.1.34

1 year ago

0.1.35

1 year ago

0.1.36

1 year ago

0.1.37

1 year ago

0.1.27

1 year ago

0.1.28

1 year ago

0.1.29

1 year ago

0.1.24

1 year ago

0.1.25

1 year ago

0.1.26

1 year ago

0.1.21

1 year ago

0.1.22

1 year ago

0.1.23

1 year ago

2.33.1

2 years ago

2.33.0

2 years ago

2.33.7

2 years ago

2.33.6

2 years ago

2.33.8

2 years ago

2.33.3

2 years ago

2.33.2

2 years ago

2.33.5

2 years ago

2.33.4

2 years ago

2.32.0

2 years ago

2.31.1

2 years ago

2.31.0

2 years ago

2.31.3

2 years ago

2.31.2

2 years ago

2.31.5

2 years ago

2.31.4

2 years ago

2.31.7

2 years ago

2.31.6

2 years ago

2.30.2

2 years ago

2.30.1

2 years ago

2.30.0

2 years ago

2.29.0

2 years ago

2.29.1

2 years ago

2.28.22

2 years ago

2.28.23

2 years ago

2.28.20

2 years ago

2.28.21

2 years ago

2.28.19

2 years ago

2.28.17

2 years ago

2.28.18

2 years ago

2.28.15

2 years ago

2.28.16

2 years ago

2.28.9

2 years ago

2.28.8

2 years ago

2.28.5

2 years ago

2.28.4

2 years ago

2.28.7

2 years ago

2.28.6

2 years ago

2.28.13

2 years ago

2.28.14

2 years ago

2.28.11

2 years ago

2.28.12

2 years ago

2.28.10

2 years ago

2.19.8

2 years ago

2.19.9

2 years ago

2.19.6

2 years ago

2.19.7

2 years ago

2.19.4

2 years ago

2.19.5

2 years ago

2.19.3

2 years ago

2.22.0

2 years ago

2.21.0

2 years ago

2.21.2

2 years ago

2.21.1

2 years ago

2.20.0

2 years ago

2.28.1

2 years ago

2.28.0

2 years ago

2.28.3

2 years ago

2.28.2

2 years ago

2.27.9

2 years ago

2.27.6

2 years ago

2.27.5

2 years ago

2.27.8

2 years ago

2.27.7

2 years ago

2.27.2

2 years ago

2.27.1

2 years ago

2.27.4

2 years ago

2.27.3

2 years ago

2.27.0

2 years ago

2.26.1

2 years ago

2.26.0

2 years ago

2.19.10

2 years ago

2.19.11

2 years ago

2.25.0

2 years ago

2.24.0

2 years ago

2.23.0

2 years ago

2.19.2

2 years ago

2.19.0

2 years ago

2.19.1

2 years ago

2.18.3

2 years ago

2.18.2

2 years ago

2.16.9

3 years ago

2.16.7

3 years ago

2.16.8

3 years ago

2.17.4

2 years ago

2.16.6

3 years ago

2.18.1

2 years ago

2.17.2

2 years ago

2.17.3

2 years ago

2.17.0

2 years ago

2.18.0

2 years ago

2.17.1

2 years ago

2.16.15

2 years ago

2.16.14

2 years ago

2.16.17

2 years ago

2.16.16

2 years ago

2.16.11

3 years ago

2.16.10

3 years ago

2.16.13

2 years ago

2.16.12

3 years ago

2.16.5

3 years ago

2.16.3

3 years ago

2.16.4

3 years ago

2.16.2

3 years ago

2.16.1

3 years ago

2.16.0

3 years ago

2.15.5

3 years ago