@navinc/base-react-components
Version:
Nav's Pattern Library
32 lines (22 loc) • 956 B
JavaScript
import React, { createContext, useContext } from 'react'
import { parseQueryString, deprecationWarning } from '@navinc/utils'
const FeatureContext = createContext()
export const FeatureProvider = ({ children, ...props }) => {
deprecationWarning(true, 'the <Feature/> and <FeatureProvider /> components are deprecated.')
return <FeatureContext.Provider {...props}>{children}</FeatureContext.Provider>
}
export const Feature = ({ children, name = '', search = '' }) => {
deprecationWarning(true, 'the <Feature/> and <FeatureProvider /> components are deprecated.')
const featureContext = useContext(FeatureContext)
if (featureContext === undefined) {
throw new Error('Feature must be used within a FeatureProvider')
}
const queryParams = parseQueryString(search)
const features = {
...featureContext,
...queryParams,
}
const isOn = ['true', true].includes(features[name])
if (!isOn) return null
return children
}