@atlaskit/theme
Version:
Theme contains solutions for global theming, colors, and other useful mixins.
78 lines (73 loc) • 4.32 kB
JavaScript
;
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 _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
/**
* 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
};
}