@mapbox/frontend-util-styles

0.3.0 • Public • Published

@mapbox/frontend-util-styles

frontend-util-styles includes two things:

  1. A set of class constants that align to Mapbox Styleguide styles.
  2. A set of utilities for using the class constants.

Installation

npm install @mapbox/frontend-util-styles

API

styles.js maintains state of a style object and exports three functions:

getStyle

Get the corresponding style rules for a given constant.

getStyle('h1');

// 'txt-h1-mm txt-h2 txt-fancy'

Pass an array of strings as an optional second argument in order to exclude classes from the style constant:

getStyle('h1', ['txt-fancy']);

// 'txt-h1-mm txt-h2'

mergeStyles

Use this method to combine a set of constants with existing constants.

mergeStyles({
  h1: 'some nice classes',
  foo: 'bar'
});

/*
_styles ->
{
  h1: 'some nice classes',
  h2: 'txt-h2-mm txt-h3 txt-fancy mb6',
  ...
  foo: 'bar'
}
*/

setStyles

Set the contents of the styles object. Overrides existing constants. Use this method if you do not want to use the default values in src/styles.js:

setStyles({ foo: 'bar' });

/*
_styles ->
{
  foo: 'bar'
}
*/

Example usage

Use the styles util to set the className property in React components:

import { mergeStyles, getStyle } from '../src/styles';

mergeStyles({
  specialHeading: 'some fun classes',
  specialParagraph: 'more fun classes'
});

class Example extends React.Component {
  render() {
    return (
    <div>
      <div className={getStyle('specialHeading')}>
      Hello world!
      </div>
      <div className={getStyle('h1', ['txt-fancy'])}>
      Goodbye world!
      </div>
    </div>);
  }
}

/*
<div>
  <div class='some fun classes'>
  Hello world!
  </div>
  <div class='txt-h1-mm txt-h2 mb6'>
  Goodbye world!
  </div>
</div>
*/

Readme

Keywords

none

Package Sidebar

Install

npm i @mapbox/frontend-util-styles

Weekly Downloads

2

Version

0.3.0

License

SEE LICENSE IN LICENSE_Mapbox

Last publish

Collaborators

  • mbx-npm-ci-production
  • mbx-npm-ci-staging
  • mbx-npm-advanced-actions-production
  • mbx-npm-advanced-actions-staging
  • mbx-npm-09-production
  • mbx-npm-08-production
  • mbx-npm-07-production
  • mbx-npm-06-production
  • mbx-npm-05-production
  • mbx-npm-04-production
  • mbx-npm-03-production
  • mbx-npm-02-production
  • mbx-npm-01-production
  • mbx-npm-02-staging
  • mapbox-npm-01
  • mapbox-npm-02
  • mapbox-npm-07
  • mapbox-npm-03
  • mapbox-npm-04
  • mapbox-npm-09
  • mapbox-npm-05
  • mapbox-npm-06
  • mapbox-npm-08
  • mapbox-npm-advanced-actions
  • mapbox-npm-ci
  • mapbox-npm
  • mapbox-admin
  • mapbox-machine-user