Skip to content

Commit

Permalink
feat(sanity): add CapabilityGate component
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Feb 12, 2025
1 parent 6c79dc5 commit e23ed66
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/sanity/src/core/components/CapabilityGate.tsx
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
}

0 comments on commit e23ed66

Please sign in to comment.