-
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(quark): add support for parent-child slots
- Loading branch information
Samuel Hobl
committed
Jun 16, 2020
1 parent
dbab1c5
commit 919b073
Showing
2 changed files
with
103 additions
and
7 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,78 @@ | ||
import * as React from 'react'; | ||
import mergeProps from 'merge-props'; | ||
import { ThemedStyle } from './interpolate'; | ||
|
||
type Slots = { [slot: string]: ThemedStyle }; | ||
|
||
const SlotContext = React.createContext<Slots | null>(null); | ||
|
||
/** | ||
* Consumes slots props from parents. | ||
* | ||
* @example | ||
* const props = useSlotProps(props, 'title') | ||
*/ | ||
export function useSlotProps<T>( | ||
props: T & { slot?: string }, | ||
defaultSlot: string, | ||
) { | ||
const slot = props.slot ?? defaultSlot; | ||
const { [slot]: slotProps = {} } = React.useContext(SlotContext) || {}; | ||
return mergeProps(slotProps, props); | ||
} | ||
|
||
/** | ||
* Consumes slots styles from parents. | ||
* | ||
* @example | ||
* const styles = useSlotStyles('title') | ||
*/ | ||
export function useSlotStyles(slot: string) { | ||
const { [slot]: slotStyles = {} } = React.useContext(SlotContext) || {}; | ||
return slotStyles; | ||
} | ||
|
||
/** | ||
* Provides slots for underlying components. | ||
* | ||
* @example | ||
* <SlotProvider slots={{ Title: { color: 'red.5' } }}> | ||
* {children} | ||
* </SlotProvider> | ||
*/ | ||
export function SlotProvider({ | ||
slots, | ||
children, | ||
}: { | ||
slots: Slots; | ||
children: React.ReactNode; | ||
}) { | ||
const parentSlots = React.useContext(SlotContext) || {}; | ||
const value = React.useMemo(() => { | ||
return Object.keys(parentSlots) | ||
.concat(Object.keys(slots)) | ||
.reduce((accumulator, current) => { | ||
return { | ||
...accumulator, | ||
[current]: mergeProps( | ||
parentSlots[current] || {}, | ||
slots[current] || {}, | ||
), | ||
}; | ||
}, {}); | ||
}, [parentSlots, slots]); | ||
|
||
return <SlotContext.Provider value={value}>{children}</SlotContext.Provider>; | ||
} | ||
|
||
/** | ||
* Resets/Clears the slots context. | ||
* | ||
* @example | ||
* <ClearSlots> | ||
* {children} | ||
* </ClearSlots> | ||
*/ | ||
export function ClearSlots({ children }: { children: React.ReactNode }) { | ||
return <SlotContext.Provider value={{}}>{children}</SlotContext.Provider>; | ||
} |
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