25.0.0 • Published 1 year ago

@guardian/source-react-components v25.0.0

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
1 year ago

@guardian/source-react-components

npm

A set of robust, accessible React components built using @guardian/source-foundations.

Install

$ yarn add @guardian/source-react-components

or

$ npm install @guardian/source-react-components

You must install react, @emotion/react and @guardian/source-foundations to use Source components.

Example

import { TextInput, Button } from '@guardian/source-react-components';

const Form = () => (
    <form>
        <TextInput label="First name" />
        <Button>Submit</Button>
    </form>
);

Theming

By default, all components are styled using their light theme.

To apply a different theme, we recommend using Emotion's theming API. You can import the desired theme from @guardian/source-react-components

To compose a theme for specific components:

import { ThemeProvider } from '@emotion/react';
import {
    Button,
    buttonThemeBrand,
    TextInput,
    textInputThemeBrand,
} from '@guardian/source-react-components';

const Form = () => (
    <ThemeProvider theme={{ ...buttonThemeBrand, ...textInputThemeBrand }}>
        <form>
            <TextInput label="First Name" />
            <Button>Click me</Button>
        </form>
    </ThemeProvider>
);

Available themes

Standard themes available from @guardian/source-react-components:

  • default – exported as [componentName]ThemeDefault
  • brand – exported as [componentName]ThemeBrand
  • brandAlt – exported as [componentName]ThemeBrandAlt

For a preview of how these themes look, visit the colour tokens section of the colour guide.

To find out which themes a component supports, check its stories under Packages/source-react-components/<ComponentName>

Note that some components have their own unique themes in addition to the standard set of themes. For instance, the button component defines two themes for use in Reader Revenue flows.

Overriding styles

To extend or override the styles of Source components, pass SerializedStyles generated by the css function to the cssOverrides prop.

import { css } from '@emotion/react';

const dangerStyles = css`
    background-colour: ${error[500]};
    color: ${text.ctaPrimary};

    &:hover {
        background-colour: ${error[400]};
    }
`;

const Danger = () => (
    <Button cssOverrides={dangerStyles}>Delete my account</Button>
);

The cssOverrides prop is mixed into the css prop for the main element of the component. This differs from component to component. You'll need to look at the source code for the component to see where your overrides will be applied.

Using classNames

If you are using a CSS solution that relies on passing the className prop, you can pass your className to the Source component as normal.

One caveat here is that @emotion/react, which Source components use for styling, appends style tags to the bottom of the document head. Since they appear at the end of the cascade, they tend to override other styles unless the other styles have a higher specificity.

You may therefore need to mark overrides as !important in order to ensure they are applied.

25.0.0

1 year ago

24.0.1

1 year ago

24.0.0

1 year ago

23.0.1

1 year ago

23.0.0

1 year ago

22.1.0

1 year ago

22.0.2

1 year ago

22.0.1

1 year ago

22.0.0

1 year ago

21.0.0

1 year ago

20.0.0

1 year ago

19.0.1

1 year ago

19.0.0

1 year ago

18.0.0

2 years ago

17.0.1

2 years ago

15.0.2

2 years ago

17.0.0

2 years ago

16.0.1

2 years ago

16.0.0

2 years ago

15.0.1

2 years ago

15.0.0

2 years ago

14.0.2

2 years ago

14.0.0

2 years ago

14.0.1

2 years ago

13.0.0

2 years ago

12.0.0

2 years ago

11.4.0

2 years ago

11.2.0

2 years ago

11.0.0

2 years ago

11.3.0

2 years ago

11.1.0

2 years ago

9.1.2

3 years ago

10.0.0

3 years ago

10.0.1

2 years ago

10.0.2

2 years ago

9.1.1

3 years ago

9.1.0

3 years ago

8.0.1

3 years ago

9.0.1

3 years ago

9.0.0

3 years ago

7.0.0

3 years ago

7.1.3

3 years ago

7.1.2

3 years ago

7.1.1

3 years ago

7.1.0

3 years ago

7.0.1

3 years ago

7.1.5

3 years ago

7.1.4

3 years ago

8.0.0

3 years ago

6.0.0

3 years ago

5.0.0

3 years ago

4.4.0

3 years ago

4.3.1

3 years ago

4.3.0

3 years ago

4.1.0

3 years ago

4.0.1

3 years ago

4.0.0-rc.5

3 years ago

4.0.0

3 years ago

4.0.0-rc.4

3 years ago

4.0.3

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

4.0.2

3 years ago

4.0.0-rc.3

4 years ago

4.0.0-rc.1

4 years ago

4.0.0-rc.0

4 years ago

4.0.0-rc.2

4 years ago

4.0.0-alpha.6

4 years ago

4.0.0-alpha.5

4 years ago

4.0.0-alpha.4

4 years ago

4.0.0-alpha.3

4 years ago

4.0.0-alpha.2

4 years ago

4.0.0-alpha.1

4 years ago

4.0.0-alpha.0

4 years ago