-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(atomic-elements): Add useSlottedContext for slots support. Updat…
…e useContextProps to use useSlottedContext instead of useContext directly
- Loading branch information
1 parent
19e6aed
commit 04bb2de
Showing
3 changed files
with
59 additions
and
12 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
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,44 @@ | ||
import { useContext } from 'react'; | ||
|
||
export type SlottedContextValue<T> = T | { | ||
slots?: Record<string | symbol, T> | ||
} | null | undefined; | ||
|
||
export interface SlotProps { | ||
slot?: string | null; | ||
} | ||
|
||
export const DEFAULT_SLOT = Symbol("DEFAULT_SLOT"); | ||
|
||
// from: https://github.com/adobe/react-spectrum/blob/c6bd2cb0808838a9f1f850b6c1ffe88465254222/packages/react-aria-components/src/utils.tsx#L157 | ||
/** Consume a context OR consume a slotted context value */ | ||
export function useSlottedContext<T>( | ||
context: React.Context<SlottedContextValue<T>>, | ||
slot?: string | null | ||
): T | null | undefined { | ||
const ctx = useContext(context); | ||
if (slot === null) { | ||
// An explicit `null` slot means don't use context. | ||
return null; | ||
} | ||
|
||
// If slots are provided by the context we need to consume the correct slot | ||
if (ctx && typeof ctx === "object" && "slots" in ctx && ctx.slots) { | ||
const allSlots = Object.keys(ctx.slots); | ||
|
||
if (!slot && !ctx.slots[DEFAULT_SLOT]) { | ||
throw new Error(`A slot prop is required. Valid slot names are ${allSlots.join(", ")}`); | ||
} | ||
|
||
const key = slot || DEFAULT_SLOT; | ||
|
||
if (!ctx.slots[key]) { | ||
throw new Error(`Invalid slot name. Valid slot names are ${allSlots.join(", ")}`); | ||
} | ||
|
||
return ctx.slots[key]; | ||
} | ||
|
||
// The context doesn't provide a slot, consume the context as is | ||
return ctx as T; | ||
} |
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 |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { SlottedContextValue } from "@hooks/useSlottedContext"; | ||
import React from "react"; | ||
|
||
export function createComponentContext<T extends object>( | ||
defaultValue: Partial<T> = {} | ||
) { | ||
return React.createContext<Partial<T & { ref?: React.Ref<any> }>>( | ||
defaultValue | ||
); | ||
export function createComponentContext<T>() { | ||
return React.createContext<SlottedContextValue<T>>(null as any); | ||
} | ||
|
||
|