UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

99 lines 4.63 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NeedleThemeProvider = exports.useNeedleTheme = exports.TokensContext = exports.ThemeContext = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); /** * * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ const react_1 = __importStar(require("react")); const defaultImports_1 = require("../_common/defaultImports"); exports.ThemeContext = (0, react_1.createContext)(undefined); exports.TokensContext = (0, react_1.createContext)(undefined); /** * Hook to access the current theme effortlessly */ const useNeedleTheme = () => { let theme = (0, react_1.useContext)(exports.ThemeContext); /** * If the theme is undefined, we are not * inside a `NeedleThemeProvider` . * Defaults to `light` theme. */ if (theme === undefined) theme = 'light'; const tokens = (0, react_1.useContext)(exports.TokensContext); return { theme, themeClassName: `ndl-theme-${theme}`, tokens, }; }; exports.useNeedleTheme = useNeedleTheme; const NeedleThemeProvider = ({ theme = 'light', tokens, children, wrapperProps, }) => { const clonedChildren = (0, react_1.useMemo)(() => { const _a = wrapperProps || {}, { isWrappingChildren, className } = _a, restWrapperProps = __rest(_a, ["isWrappingChildren", "className"]); const props = Object.assign(Object.assign({}, restWrapperProps), { className: (0, defaultImports_1.classNames)(className, `ndl-theme-${theme}`) }); if (isWrappingChildren !== true) { return react_1.default.Children.map(children, (child) => { return react_1.default.cloneElement(child, Object.assign(Object.assign({}, props), { className: (0, defaultImports_1.classNames)(child.props.className, props.className) })); }); } else { return ((0, jsx_runtime_1.jsx)("span", Object.assign({}, props, { className: (0, defaultImports_1.classNames)('ndl-theme-wrapper', props.className), children: children }))); } }, [wrapperProps, theme, children]); return ((0, jsx_runtime_1.jsx)(exports.ThemeContext.Provider, { value: theme, children: (0, jsx_runtime_1.jsx)(exports.TokensContext.Provider, { value: tokens, children: clonedChildren }) })); }; exports.NeedleThemeProvider = NeedleThemeProvider; //# sourceMappingURL=ThemeProvider.js.map