Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX base theme initialization and theme bootup #5843

Merged
merged 20 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
24f9ec6
ADD ability for users to specify baseTheme, and the theme createFn wi…
ndelangen Mar 4, 2019
aa03da5
FIX circular dependencies
ndelangen Mar 4, 2019
32a6982
Merge branch 'next' into tech/improve-theme-creating
ndelangen Mar 4, 2019
5373dba
RENAME ThemeVar to ThemeVars && REMOVE the pick & p function
ndelangen Mar 4, 2019
c7fbf47
FIX issue where the `base` property wouldn't be set if non was given
ndelangen Mar 4, 2019
2aad74c
CLEANUP the creation of default themes
ndelangen Mar 4, 2019
04b5071
MOVE ThemeVars to base && FIX custom properties overriding known prop…
ndelangen Mar 4, 2019
caa78fb
ADD tests
ndelangen Mar 4, 2019
1c27092
FIX a bug with animations not being stringify-able
ndelangen Mar 4, 2019
dd3c382
REFACTOR change the theme that users import & set to be ThemeVars (wa…
ndelangen Mar 4, 2019
13b2257
OPTIMIZE so the theme doesn't update every story change
ndelangen Mar 4, 2019
814471a
Merge branch 'next' into tech/improve-theme-creating
ndelangen Mar 4, 2019
8b28b30
FIX tests
ndelangen Mar 4, 2019
fac9ee3
FIX incorrect optimization
ndelangen Mar 4, 2019
2aec10a
ADD special case for `barSelectedColor`, since it should be using `co…
ndelangen Mar 4, 2019
ddead74
FIX the location of the default theme on state
Mar 5, 2019
e5696dc
Ensure the theme is insta-loaded from local storage
Mar 5, 2019
eeb024f
FIX Don't retain persisted UI state
Mar 5, 2019
be7646e
Merge pull request #5856 from storybooks/tech/load-initial-theme
shilman Mar 5, 2019
52968c9
Merge pull request #5858 from storybooks/tech/workaround-persisted-op…
shilman Mar 5, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions addons/knobs/src/components/__tests__/Panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { shallow, mount } from 'enzyme';
import { STORY_CHANGED } from '@storybook/core-events';
import { TabsState } from '@storybook/components';

import { ThemeProvider, themes } from '@storybook/theming';
import { ThemeProvider, themes, convert } from '@storybook/theming';
import Panel from '../Panel';
import { CHANGE, SET } from '../../shared';
import PropForm from '../PropForm';
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('Panel', () => {
// We have to do a full mount.

const root = mount(
<ThemeProvider theme={themes.light}>
<ThemeProvider theme={convert(themes.light)}>
<Panel channel={testChannel} api={testApi} active />
</ThemeProvider>
);
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('Panel', () => {

it('should have one tab per groupId and an empty ALL tab when all are defined', () => {
const root = mount(
<ThemeProvider theme={themes.light}>
<ThemeProvider theme={convert(themes.light)}>
<Panel channel={testChannel} api={testApi} active />
</ThemeProvider>
);
Expand Down Expand Up @@ -265,7 +265,7 @@ describe('Panel', () => {

it('the ALL tab should have its own additional content when there are knobs both with and without a groupId', () => {
const root = mount(
<ThemeProvider theme={themes.light}>
<ThemeProvider theme={convert(themes.light)}>
<Panel channel={testChannel} api={testApi} active />
</ThemeProvider>
);
Expand Down
7 changes: 4 additions & 3 deletions examples/official-storybook/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { storiesOf, configure, addDecorator, addParameters } from '@storybook/react';
import { Global, ThemeProvider, themes, createReset } from '@storybook/theming';
import { Global, ThemeProvider, themes, createReset, create, convert } from '@storybook/theming';

import { withCssResources } from '@storybook/addon-cssresources';
import { withA11y } from '@storybook/addon-a11y';
Expand Down Expand Up @@ -32,7 +32,7 @@ addDecorator(withA11y);
addDecorator(withNotes);

addDecorator(storyFn => (
<ThemeProvider theme={themes.normal}>
<ThemeProvider theme={convert(themes.light)}>
<Global styles={createReset} />
{storyFn()}
</ThemeProvider>
Expand All @@ -49,9 +49,10 @@ addParameters({
options: {
hierarchySeparator: /\/|\./,
hierarchyRootSeparator: '|',
theme: create({ colorPrimary: 'hotpink', colorSecondary: 'orangered' }),
},
backgrounds: [
{ name: 'storybook app', value: themes.normal.background.app, default: true },
{ name: 'storybook app', value: themes.light.appBg, default: true },
{ name: 'light', value: '#eeeeee' },
{ name: 'dark', value: '#222222' },
],
Expand Down
36 changes: 36 additions & 0 deletions lib/theming/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,42 @@ export const typography = {
},
};

export interface ThemeVars {
base: 'light' | 'dark';

colorPrimary?: string;
colorSecondary?: string;

// UI
appBg?: string;
appContentBg?: string;
appBorderColor?: string;
appBorderRadius?: number;

// Typography
fontBase?: string;
fontCode?: string;

// Text colors
textColor?: string;
textInverseColor?: string;

// Toolbar default and active colors
barTextColor?: string;
barSelectedColor?: string;
barBg?: string;

// Form colors
inputBg?: string;
inputBorder?: string;
inputTextColor?: string;
inputBorderRadius?: number;

brandTitle?: string;
brandUrl?: string;
brandImage?: string;
}

export type Color = typeof color;
export type Background = typeof background;
export type Typography = typeof typography;
Expand Down
234 changes: 122 additions & 112 deletions lib/theming/src/create.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,21 @@
// This generates theme variables in the correct shape for the UI

import { Theme, Brand, color, Color, background, typography } from './base';
import { easing, animation } from './animation';
import { create as createSyntax } from './modules/syntax';
import { chromeLight, chromeDark } from 'react-inspector';
import { opacify } from 'polished';

interface Rest {
[key: string]: any;
}

interface ThemeVar {
base?: 'light' | 'dark';
import lightThemeVars from './themes/light';
import darkThemeVars from './themes/dark';

colorPrimary?: string;
colorSecondary?: string;

// UI
appBg?: string;
appContentBg?: string;
appBorderColor?: string;
appBorderRadius?: number;

// Typography
fontBase?: string;
fontCode?: string;

// Text colors
textColor?: string;
textInverseColor?: string;

// Toolbar default and active colors
barTextColor?: string;
barSelectedColor?: string;
barBg?: string;
import { Theme, color, Color, background, typography, ThemeVars } from './base';
import { easing, animation } from './animation';
import { create as createSyntax } from './modules/syntax';

// Form colors
inputBg?: string;
inputBorder?: string;
inputTextColor?: string;
inputBorderRadius?: number;
const themes: { light: ThemeVars; dark: ThemeVars } = { light: lightThemeVars, dark: darkThemeVars };

brandTitle?: string;
brandUrl?: string;
brandImage?: string;
interface Rest {
[key: string]: any;
}

const createColors = (vars: ThemeVar): Color => ({
const createColors = (vars: ThemeVars): Color => ({
// Changeable colors
primary: vars.colorPrimary,
secondary: vars.colorSecondary,
Expand Down Expand Up @@ -110,76 +79,117 @@ const darkSyntaxColors = {
blue2: '#00009f',
};

export const create = (vars: ThemeVar, rest?: Rest): Theme => ({
base: vars.base,
color: createColors(vars),
background: {
app: vars.appBg || background.app,
content: vars.appContentBg || color.lightest,
hoverable: vars.base === 'light' ? 'rgba(0,0,0,.05)' : 'rgba(250,250,252,.1)' || background.hoverable,

positive: background.positive,
negative: background.negative,
warning: background.warning,
},
typography: {
fonts: {
base: vars.fontBase || typography.fonts.base,
mono: vars.fontCode || typography.fonts.mono,
export const create = (vars: ThemeVars = { base: 'light' }, rest?: Rest): ThemeVars => {
const inherit: ThemeVars = {
...themes.light,
...(themes[vars.base] || {}),
...vars,
...{ base: themes[vars.base] ? vars.base : 'light' },
};
return {
...rest,
...inherit,
...{ barSelectedColor: vars.barSelectedColor || inherit.colorSecondary },
};
};

export const convert = (inherit: ThemeVars = lightThemeVars): Theme => {
const {
base,
colorPrimary,
colorSecondary,
appBg,
appContentBg,
appBorderColor,
appBorderRadius,
fontBase,
fontCode,
textColor,
textInverseColor,
barTextColor,
barSelectedColor,
barBg,
inputBg,
inputBorder,
inputTextColor,
inputBorderRadius,
brandTitle,
brandUrl,
brandImage,
...rest
} = inherit;

return {
...(rest || {}),

base,
color: createColors(inherit),
background: {
app: appBg,
content: appContentBg,
hoverable: base === 'light' ? 'rgba(0,0,0,.05)' : 'rgba(250,250,252,.1)' || background.hoverable,

positive: background.positive,
negative: background.negative,
warning: background.warning,
},
weight: typography.weight,
size: typography.size,
},
animation,
easing,

input: {
border: vars.inputBorder || color.border,
background: vars.inputBg || color.lightest,
color: vars.inputTextColor || color.defaultText,
borderRadius: vars.inputBorderRadius || vars.appBorderRadius || 4,
},

// UI
layoutMargin: 10,
appBorderColor: vars.appBorderColor || color.border,
appBorderRadius: vars.appBorderRadius || 4,

// Toolbar default/active colors
barTextColor: vars.barTextColor || color.mediumdark,
barSelectedColor: vars.barSelectedColor || color.secondary,
barBg: vars.barBg || color.lightest,

// Brand logo/text
brand: {
title: vars.brandTitle,
url: vars.brandUrl,
image: vars.brandImage,
},

code: createSyntax({
colors: vars.base === 'light' ? lightSyntaxColors : darkSyntaxColors,
mono: vars.fontCode || typography.fonts.mono,
}),

// Addon actions theme
// API example https://github.com/xyc/react-inspector/blob/master/src/styles/themes/chromeLight.js
addonActionsTheme: {
...(vars.base === 'light' ? chromeLight : chromeDark),

BASE_FONT_FAMILY: vars.fontCode || typography.fonts.mono,
BASE_FONT_SIZE: typography.size.s2 - 1,
BASE_LINE_HEIGHT: '18px',
BASE_BACKGROUND_COLOR: 'transparent',
BASE_COLOR: vars.textColor || color.darkest,
ARROW_COLOR: opacify(0.2, vars.appBorderColor || color.border),
ARROW_MARGIN_RIGHT: 4,
ARROW_FONT_SIZE: 8,
TREENODE_FONT_FAMILY: vars.fontCode || typography.fonts.mono,
TREENODE_FONT_SIZE: typography.size.s2 - 1,
TREENODE_LINE_HEIGHT: '18px',
TREENODE_PADDING_LEFT: 12,
},

...(rest || {}),
});
typography: {
fonts: {
base: fontBase,
mono: fontCode,
},
weight: typography.weight,
size: typography.size,
},
animation,
easing,

input: {
border: inputBorder,
background: inputBg,
color: inputTextColor,
borderRadius: inputBorderRadius,
},

// UI
layoutMargin: 10,
appBorderColor,
appBorderRadius,

// Toolbar default/active colors
barTextColor,
barSelectedColor: barSelectedColor || colorSecondary,
barBg,

// Brand logo/text
brand: {
title: brandTitle,
url: brandUrl,
image: brandImage,
},

code: createSyntax({
colors: base === 'light' ? lightSyntaxColors : darkSyntaxColors,
mono: fontCode,
}),

// Addon actions theme
// API example https://github.com/xyc/react-inspector/blob/master/src/styles/themes/chromeLight.js
addonActionsTheme: {
...(base === 'light' ? chromeLight : chromeDark),

BASE_FONT_FAMILY: fontCode,
BASE_FONT_SIZE: typography.size.s2 - 1,
BASE_LINE_HEIGHT: '18px',
BASE_BACKGROUND_COLOR: 'transparent',
BASE_COLOR: textColor,
ARROW_COLOR: opacify(0.2, appBorderColor),
ARROW_MARGIN_RIGHT: 4,
ARROW_FONT_SIZE: 8,
TREENODE_FONT_FAMILY: fontCode,
TREENODE_FONT_SIZE: typography.size.s2 - 1,
TREENODE_LINE_HEIGHT: '18px',
TREENODE_PADDING_LEFT: 12,
},
};
};
Loading