-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sanity): add
CapabilityGate
component
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {type ComponentType, type PropsWithChildren} from 'react' | ||
import {useObservable} from 'react-rx' | ||
|
||
import {useRenderingContextStore} from '../store/_legacy/datastores' | ||
import {type Capability} from '../store/renderingContext/types' | ||
|
||
type Props = PropsWithChildren<{ | ||
capability: Capability | ||
}> | ||
|
||
/** | ||
* `CapabilityGate` only renders its children if the current Studio rendering context does not | ||
* provide the specified capability. | ||
* | ||
* This allows consumers of the component to conveniently mark a portion of the React tree as | ||
* providing a capability that may be overriden by the Studio rendering context. If the rendering | ||
* context provides this capability, the local implementation will not be rendered. | ||
*/ | ||
export const CapabilityGate: ComponentType<Props> = ({children, capability}) => { | ||
const renderingContextStore = useRenderingContextStore() | ||
const renderingContextCapabilities = useObservable(renderingContextStore.capabilities, {}) | ||
const renderingContextHasCapability = renderingContextCapabilities[capability] === true | ||
|
||
if (renderingContextHasCapability) { | ||
return null | ||
} | ||
|
||
return children | ||
} |