UNPKG

@atlaskit/theme

Version:

Theme contains solutions for global theming, colors, and other useful mixins.

65 lines (61 loc) 3.09 kB
import React, { createContext, useCallback, useContext } from 'react'; /** * createTheme is used to create a set of Providers and Consumers for theming components. * - Takes a default theme function; this theme function gets a set of props, and returns tokens * based on those props. An example of this default theme function is one that produces the standard * appearance of the component * - Returns three things - a Provider that allow for additional themes to be applied, a Consumer * that can get the current theme and fetch it, and a custom hook - useTheme which provides an alternate (although functionally the same) API * to the Consumer. */ export function createTheme(defaultGetTokens) { const emptyThemeFn = (getTokens, props) => getTokens(props); /** * Internally, Theme uses React Context, with internal providers and consumers. * The React Context passes only a function that gets props, and turns them into tokens. This * function gets mixed as more Providers with their own themes are added. This mixed function * is ultimately picked up by Consumers, which implement a context consumer internally to fetch * the theme. */ const ThemeContext = /*#__PURE__*/createContext(defaultGetTokens); function useTheme(themeProps) { const theme = useContext(ThemeContext); const themeFn = theme || emptyThemeFn; const tokens = themeFn(themeProps); return tokens; } // The Theme Consumer takes a function as its child - this function takes tokens, and the // return value is generally a set of nodes with the tokens applied appropriately. function Consumer(props) { const { children, ...themeProps } = props; // @ts-ignore See issue for more info: https://github.com/Microsoft/TypeScript/issues/10727 // Argument of type 'Pick<ThemeProps & { children: (tokens: ThemeTokens) => ReactNode; }, Exclude<keyof ThemeProps, "children">>' is not assignable to parameter of type 'ThemeProps'.ts(2345) const tokens = useTheme(themeProps); // We add a fragment to ensure we don't break people upgrading. // Previously they may have been able to pass in undefined without things blowing up. return /*#__PURE__*/React.createElement(React.Fragment, null, children(tokens)); } /** * The Theme Provider takes regular nodes as its children, but also takes a *theme function* * - The theme function takes a set of props, as well as a function (getTokens) that can turn props into tokens. * - The getTokens function isn't called immediately - instead the props are passed * through a mix of parent theming functions * Children of this provider will receive this mixed theme */ function Provider(props) { const themeFn = useContext(ThemeContext); const valueFn = props.value || emptyThemeFn; const mixedFn = useCallback(themeProps => valueFn(themeFn, themeProps), [themeFn, valueFn]); return /*#__PURE__*/React.createElement(ThemeContext.Provider, { value: mixedFn }, props.children); } return { Consumer, Provider, useTheme }; }