2.0.2 • Published 4 months ago

brandeur v2.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

Brandeur

Brandeur is a convenience layer and tool belt on top of css-hooks for React.

Benefits

  • Similar API
  • Theming
  • RTL Conversion
  • Vendor Prefixing
  • Fallback Value Support
  • Keyframes Support
  • Extendable Plugin System
  • Fela Plugin Compatibility
  • TypeScript Support

Installation

# npm
npm i --save brandeur
# yarn
yarn add brandeur
# pnpm
pnpm add brandeur

The Gist

import { createHooks } from 'brandeur'
import prefixer, { fallbacks } from 'brandeur-plugin-prefixer'

const theme = {
  colors: { primary: 'red' },
}

const [staticCSS, css] = createHooks({
  hooks: {
    // either custom hooks or @css-hooks/recommended
  },
  plugins: [prefixer()] as const,
  fallbacks: [
    ...fallbacks,
    { property: 'position', values: ['-webkit-sticky', 'sticky'] },
  ],
  keyframes: {
    fadeIn: {
      from: { opacity: 0 },
      to: { opacity: 1 },
    },
  },
  theme,
})

// fades in, red color, vendor prefixed and browser-compatible position: sticky
const style = css(({ theme, keyframes }) => ({
  animationName: keyframes.fadeIn,
  color: theme.colors.primary,
  position: 'sticky',
  appearance: 'none',
}))

Why

tbd.

API

Currently Brandeur only exposes two functions:

createHooks

The main API that wraps createHooks from css-hooks. It returns the same structural array including both the static CSS and the css function. The difference is that the static CSS includes more than just the hooks and the css function also accepts functions.

Arguments

It accepts the following arguments as an object.

ArgumentTypeDescription
pluginsArray\List of plugins that are used to process each style before transforming into a flat hooks style. See Plugins for a list of all available plugins.
fallbacksArray\List of fallbacks that are added and automatically replace within style objects.
keyframesObjectA map of animationName-keyframe pairs.
themeObjectA theme object that can be accessed from within style functions.
Plugin
type Plugin = style => style

Plugins are simple functions that take a style object and return a new one, similar to Fela plugins.

Note: See Plugins for a list of all available plugins.

Fallback
type Fallback = {
  property: string | Array<string>
  values: Array<string>
  match?: string
}

Tip: The fallbackValue API provides a convenient way to create those fallback objects.

Returns

It returns an array where the first item is a static CSS string and the second item is the css function to create styles. The css function accepts both style object as well as functions where the first argument is an object that only has a theme property.

Example

See The Gist.

fallbackValue

A tiny helper function to create fallbacks in a more convenient way.

Arguments

It accepts the following arguments as an object.

ArgumentTypeDescription
propertyArray\ | stringA property or list of properties for which the fallback value applies.
valuesArray\A list of fallback values where the last one is the default.
matchstring?An optional matcher string that's used to replace values in style objects. Defaults to the last values value.

Returns

(Fallback) An object with respective fallback properties.

Example

import { fallbackValue } from 'brandeur'

const positionSticky = fallbackValue('position', ['-webkit-sticky', 'sticky'])

Plugins

Brandeur becomes really powerful when utilising the rich plugin echosystem. That way, we can extend the styling engine to support our personal needs.

Brandeur Plugins

NameDescription
brandeur-plugin-debugUses styles-debugger to visually debug styles.
brandeur-plugin-enforce-longhandSpecific implementation of sort-property. Enforces longhand over shorthand properties for more deterministic results.
brandeur-plugin-prefixerAdds vendor prefixes to style objects.
brandeur-plugin-sort-propertySorts properties according to a priorty map. Helpful when trying to enforce certain properties over others.
brandeur-plugin-responsive-valueResolves responsive array values.

Fela Plugins

Thanks to similar architecture and API design, brandeur supports almost all Fela plugins out of the box.

Tip: In order to get proper types, make sure that you import brandeur in the same file where Fela plugins are imported as types for all Fela plugins are shipped with the core package.

NameDescriptionCompatibility
fela-plugin-bidiEnables direction-independent styles by converting them to either rtl or ltr on the fly.Does not support context-specific direction via theme.
fela-plugin-custom-propertyResolves custom properties.Full
fela-plugin-expand-shorthandExpands shorthand properties into their longhand forms.Full
fela-plugin-extendAdds a convenient syntax for (conditionally) extending styles.Full
fela-plugin-hover-mediaWraps :hover styles in @media (hover: hover) queries.Full
fela-plugin-kebab-caseConverts properties written in kebab-case to camelCase.Full
fela-plugin-loggerLogs processed style objects.Full
fela-plugin-multiple-selectorsResolves multiple comma-separated selectors to individual object keys.Full
fela-plugin-responsive-valueResolves array values to pre-defined media queries. Useful for component APIs.Does not support the props argument to receive the theme. Use a static theme instead.
fela-plugin-rtlConverts styles to their right-to-left counterpartDoes not support context-specific direction via theme.
fela-plugin-unitAutomatically adds units to values if needed.Full
fela-plugin-validatorValidates, logs & optionally deletes invalid properties for keyframes and rules.Full

Incompatible Plugins

PluginAlternative
fela-plugin-prefixerUse brandeur-plugin-prefixer instead.
fela-plugin-named-keysfela-plugin-friendly-pseudo-classfela-plugin-pseudo-prefixerfela-plugin-fullscreen-prefixerfela-plugin-placeholder-prefixerUse hooks directly to set those.
fela-plugin-embeddedNo replacement yet due to missing font and keyframe primitives.
fela-plugin-theme-valueNo replacement yet due to incompatible plugin APIs.

TypeScript

Brandeur comes with native TypeScript support, but requires some manual setup to get the correct types for all plugins.

Style Object

By default, Brandeur automatically infers the type just like css-hooks does. It supports all React.CSSProperties. For convenience, we also export a Style type from brandeur.

In order to extend the type with plugins, we need to set them up correctly.

import { createHooks } from 'brandeur'

import extend from 'fela-plugin-extend'
import responsiveValue, {
  ResponsiveStyle,
} from 'brandeur-plugin-responsive-value'

const [staticCSS, css] = createHooks({
  // ... config
  plugins: [
    // allow responsvie array values in extended styles
    extend<ResponsiveStyle>(),
    responsiveValue([
      '@media (min-width: 480px)',
      '@media (min-width: 1024px)',
    ]),
    prefixer(),
    // use "as const" to get fixed types
  ] as const,
})

Fela Plugins

Make sure to import brandeur in your configuration file to load all the plugin types for Fela plugins. Otherwise you'll get the native ones from Fela which have conflicting types.

// load types
import 'brandeur'

import extend from 'fela-plugin-extend'
import hoverMedia from 'fela-plugin-hover-media'

Keyframes

Brandeur provides a simple way to create and consume global keyframes. Check the gist for an example.

Sometimes however we want to have dynamic keyframes and/or not render all keyframes upfront. In such cases, we recommend using a separate package to deal with that. I created react-create-keyframe for exactly those use cases. Feel free to check it out!

Roadmap

  • Theming Primitives
  • Framework-Agnostic API

License

Brandeur is licensed under the MIT License. Documentation is licensed under Creative Commons License. Created with ♥ by @robinweser.

2.0.2

4 months ago

2.0.1

9 months ago

2.0.0

10 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago