{"version":3,"file":"ThemeProvider.mjs","names":[],"sources":["../../../src/theme/ThemeProvider.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport classNames from 'classnames';\nimport React, {\n  createContext,\n  type HTMLAttributes,\n  type PropsWithChildren,\n  useContext,\n  useMemo,\n} from 'react';\ntype ThemeContextType = 'light' | 'dark';\n\nexport const ThemeContext = createContext<ThemeContextType | undefined>(\n  undefined,\n);\n\ntype TokensContextType = {\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  [key: string]: any;\n};\n\nexport const TokensContext = createContext<TokensContextType | undefined>(\n  undefined,\n);\n\ntype NeedleThemeHook = () => {\n  theme: ThemeContextType;\n  /**\n   * A classname that can be used to scope\n   * elements that are portal-ed like\n   * Modals, Popovers, etc.\n   */\n  themeClassName: string;\n  tokens?: TokensContextType;\n};\n/**\n * Hook to access the current theme effortlessly\n */\nexport const useNeedleTheme: NeedleThemeHook = () => {\n  let theme = useContext(ThemeContext);\n  /**\n   * If the theme is undefined, we are not\n   * inside a `NeedleThemeProvider` .\n   * Defaults to `light` theme.\n   */\n  if (theme === undefined) {\n    theme = 'light';\n  }\n\n  const tokens = useContext(TokensContext);\n\n  return {\n    theme,\n    themeClassName: `ndl-theme-${theme}`,\n    tokens,\n  };\n};\n\ninterface NeedleThemeProviderProps extends PropsWithChildren {\n  theme?: 'dark' | 'light';\n  tokens?: TokensContextType;\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  children: any;\n  /**\n   * Nested theme providers will receive a wrapping `span` tag\n   * in order to set correctly the default text `color` that all nested\n   * children will inherit by adding the respective class `ndl-theme-{theme}`.\n   *\n   * If an extra wrapper is not desired, pass `{ isWrappingChildren : false }`.\n   * This requires child components accept and consumer className prop.\n   *\n   */\n  wrapperProps?: HTMLAttributes<HTMLElement> & { isWrappingChildren?: boolean };\n}\n\nexport const NeedleThemeProvider = ({\n  theme = 'light',\n  tokens,\n  children,\n  wrapperProps,\n}: NeedleThemeProviderProps) => {\n  const clonedChildren = useMemo(() => {\n    const { isWrappingChildren, className, ...restWrapperProps } =\n      wrapperProps || {};\n    const props = {\n      ...restWrapperProps,\n      className: classNames(className, `ndl-theme-${theme}`),\n    };\n\n    if (isWrappingChildren !== true) {\n      return React.Children.map(children, (child) => {\n        return React.cloneElement(child, {\n          ...props,\n          className: classNames(child.props.className, props.className),\n        });\n      });\n    } else {\n      return (\n        <span\n          {...props}\n          className={classNames('ndl-theme-wrapper', props.className)}\n        >\n          {children}\n        </span>\n      );\n    }\n  }, [wrapperProps, theme, children]);\n\n  return (\n    <ThemeContext.Provider value={theme}>\n      <TokensContext.Provider value={tokens}>\n        {clonedChildren}\n      </TokensContext.Provider>\n    </ThemeContext.Provider>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAoGY,MAAA,YAAA,CAAA,sBAAoB,WAAA;AAtEhC,MAAa,eAAe,cAC1B,KAAA,CACF;AAOA,MAAa,gBAAgB,cAC3B,KAAA,CACF;;;;AAeA,MAAa,uBAAwC;CACnD,IAAI,QAAQ,WAAW,YAAY;;;;;;CAMnC,IAAI,UAAU,KAAA,GACZ,QAAQ;CAGV,MAAM,SAAS,WAAW,aAAa;CAEvC,OAAO;EACL;EACA,gBAAgB,aAAa;EAC7B;CACF;AACF;AAmBA,MAAa,uBAAuB,EAClC,QAAQ,SACR,QACA,UACA,mBAC8B;CAC9B,MAAM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