UNPKG

@atlaskit/theme

Version:

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

79 lines (74 loc) 4.49 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.createTheme = createTheme; var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _react = _interopRequireWildcard(require("react")); var _excluded = ["children"]; function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /** * 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. */ function createTheme(defaultGetTokens) { var emptyThemeFn = function emptyThemeFn(getTokens, props) { return 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. */ var ThemeContext = /*#__PURE__*/(0, _react.createContext)(defaultGetTokens); function useTheme(themeProps) { var theme = (0, _react.useContext)(ThemeContext); var themeFn = theme || emptyThemeFn; var 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) { var children = props.children, themeProps = (0, _objectWithoutProperties2.default)(props, _excluded); // @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) var 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.default.createElement(_react.default.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) { var themeFn = (0, _react.useContext)(ThemeContext); var valueFn = props.value || emptyThemeFn; var mixedFn = (0, _react.useCallback)(function (themeProps) { return valueFn(themeFn, themeProps); }, [themeFn, valueFn]); return /*#__PURE__*/_react.default.createElement(ThemeContext.Provider, { value: mixedFn }, props.children); } return { Consumer: Consumer, Provider: Provider, useTheme: useTheme }; }