@cerberus-design/react
Version:
The Cerberus Design React component library.
1 lines • 2 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/context/feature-flags.tsx"],"sourcesContent":["'use client'\n\nimport { createContext, useContext, type PropsWithChildren } from 'react'\n\n/**\n * This module provides a context and hook for feature flags.\n * @module\n */\n\nexport interface FeatureFlagValue {\n [key: string]: boolean\n}\n\nconst FeatureFlagContext = createContext<FeatureFlagValue | null>(null)\n\nexport interface FeatureFlagProviderProps {\n flags: FeatureFlagValue\n}\n\n/**\n * Provides feature flags to the application.\n * @see https://cerberus.digitalu.design/react/feature-flags\n * @example\n * ```tsx\n * // This should be a JSON file or a server response.\n * const flags = {\n * featureOne: true,\n * featureTwo: false\n * }\n *\n * // Wrap the Provider around the root of your application.\n * <FeatureFlags flags={flags}>\n * <FeatureFlag flag=\"featureOne\">\n * This is visible.\n * </FeatureFlag>\n * <FeatureFlag flag=\"featureTwo\">\n * This is hidden.\n * </FeatureFlag>\n * </FeatureFlags>\n * ```\n */\nexport function FeatureFlags(\n props: PropsWithChildren<FeatureFlagProviderProps>,\n) {\n return (\n <FeatureFlagContext.Provider value={props.flags}>\n {props.children}\n </FeatureFlagContext.Provider>\n )\n}\n\n/**\n * Used to retrieve the context of the FeatureFlags provider.\n */\nexport function useFeatureFlags(key: string): boolean {\n const context = useContext(FeatureFlagContext)\n if (context === null) {\n throw new Error(\n 'useFeatureFlag must be used within a FeatureFlags Provider',\n )\n }\n return context[key] ?? false\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAAkE;AA2C9D;AAhCJ,IAAM,yBAAqB,4BAAuC,IAAI;AA4B/D,SAAS,aACd,OACA;AACA,SACE,4CAAC,mBAAmB,UAAnB,EAA4B,OAAO,MAAM,OACvC,gBAAM,UACT;AAEJ;AAKO,SAAS,gBAAgB,KAAsB;AACpD,QAAM,cAAU,yBAAW,kBAAkB;AAC7C,MAAI,YAAY,MAAM;AACpB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO,QAAQ,GAAG,KAAK;AACzB;","names":[]}