feat(react-base16-styling)!: add invertTheme helper (#565)

BREAKING CHANGE: rename previous invertTheme to invertBase16Theme
This commit is contained in:
Nathan Bierema 2020-08-08 10:53:07 -04:00 committed by GitHub
parent 7497595b81
commit 92d16e4f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 9 deletions

View File

@ -82,10 +82,18 @@ function(themeOrStyling, base16Themes)
Helper method that returns `base16` theme object if `themeOrStyling` is `base16` theme name or theme object. Helper method that returns `base16` theme object if `themeOrStyling` is `base16` theme name or theme object.
## `invertBase16Theme`
```js
function(base16Theme)
```
Helper method that inverts `base16` theme colors, creating light theme out of dark one or vice versa.
## `invertTheme` ## `invertTheme`
```js ```js
function(theme) function(theme)
``` ```
Helper method that inverts `base16` theme colors, creating light theme out of dark one or vice versa. Helper method that inverts a theme or styling object to be passed into the `themeOrStyling` parameter of [createStyling](#createstyling).

View File

@ -148,14 +148,14 @@ const getStylingByKeys = (mergedStyling, keys, ...args) => {
return props; return props;
}; };
export const invertTheme = theme => export const invertBase16Theme = base16Theme =>
Object.keys(theme).reduce( Object.keys(base16Theme).reduce(
(t, key) => ( (t, key) => (
(t[key] = /^base/.test(key) (t[key] = /^base/.test(key)
? invertColor(theme[key]) ? invertColor(base16Theme[key])
: key === 'scheme' : key === 'scheme'
? theme[key] + ':inverted' ? base16Theme[key] + ':inverted'
: theme[key]), : base16Theme[key]),
t t
), ),
{} {}
@ -204,9 +204,27 @@ export const getBase16Theme = (theme, base16Themes) => {
const [themeName, modifier] = theme.split(':'); const [themeName, modifier] = theme.split(':');
theme = (base16Themes || {})[themeName] || base16[themeName]; theme = (base16Themes || {})[themeName] || base16[themeName];
if (modifier === 'inverted') { if (modifier === 'inverted') {
theme = invertTheme(theme); theme = invertBase16Theme(theme);
} }
} }
return theme && theme.hasOwnProperty('base00') ? theme : undefined; return theme && theme.hasOwnProperty('base00') ? theme : undefined;
}; };
export const invertTheme = theme => {
if (typeof theme === 'string') {
return `${theme}:inverted`;
}
if (theme && theme.extend) {
if (typeof theme.extend === 'string') {
return { ...theme, extend: `${theme.extend}:inverted` };
}
return { ...theme, extend: invertBase16Theme(theme.extend) };
}
if (theme) {
return invertBase16Theme(theme);
}
};

View File

@ -1,4 +1,4 @@
import { createStyling, invertTheme, getBase16Theme } from '../src'; import { createStyling, invertBase16Theme, getBase16Theme } from '../src';
import apathy from 'base16/lib/apathy'; import apathy from 'base16/lib/apathy';
const base16Theme = { const base16Theme = {
@ -86,7 +86,7 @@ const getStylingFromBase16 = base16 => ({
}); });
test('invertTheme', () => { test('invertTheme', () => {
expect(invertTheme(base16Theme)).toEqual(invertedBase16Theme); expect(invertBase16Theme(base16Theme)).toEqual(invertedBase16Theme);
}); });
test('getBase16Theme', () => { test('getBase16Theme', () => {