-
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.
- Loading branch information
Showing
7 changed files
with
198 additions
and
2 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,60 @@ | ||
import React, { useId, useState } from 'react' | ||
import { createHost, createSlot } from 'create-slots/rfc' | ||
|
||
const Description = (props: React.ComponentPropsWithoutRef<'div'>) => ( | ||
<div | ||
{...props} | ||
style={{ borderLeft: '4px solid lightgray', paddingLeft: 4 }} | ||
/> | ||
) | ||
|
||
const TextFieldLabel = createSlot('label') | ||
const TextFieldInput = createSlot('input') | ||
const TextFieldDescription = createSlot(Description) | ||
|
||
const StyledLabel = (props: React.ComponentPropsWithoutRef<'label'>) => ( | ||
<TextFieldLabel {...props} style={{ color: 'red' }} /> | ||
) | ||
|
||
export const TextField = (props: React.ComponentPropsWithoutRef<'div'>) => { | ||
const id = useId() | ||
const [value, setValue] = useState('') | ||
|
||
if (value === 'a') return null | ||
|
||
return ( | ||
<div {...props}> | ||
{createHost(props.children, (Slots) => { | ||
const labelProps = Slots.getProps(TextFieldLabel) | ||
const inputProps = Slots.getProps(TextFieldInput) | ||
const descriptionIdProps = Slots.getProps(TextFieldDescription) | ||
|
||
const inputId = inputProps?.id || `${id}-label` | ||
const descriptionId = descriptionIdProps ? `${id}-desc` : undefined | ||
|
||
return ( | ||
<> | ||
{labelProps && <label {...labelProps} htmlFor={inputId} />} | ||
{inputProps && ( | ||
<input | ||
value={value} | ||
onChange={(e) => setValue(e.target.value)} | ||
id={inputId} | ||
aria-describedby={descriptionId} | ||
{...inputProps} | ||
/> | ||
)} | ||
{descriptionIdProps && ( | ||
<Description id={descriptionId} {...descriptionIdProps} /> | ||
)} | ||
</> | ||
) | ||
})} | ||
</div> | ||
) | ||
} | ||
|
||
TextField.Label = TextFieldLabel | ||
TextField.Input = TextFieldInput | ||
TextField.Description = TextFieldDescription | ||
TextField.StyledLabel = StyledLabel |
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
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,15 @@ | ||
{ | ||
"main": "../dist/rfc/index.js", | ||
"module": "../dist/rfc/index.mjs", | ||
"types": "../dist/rfc/index.d.ts", | ||
"exports": { | ||
"development": { | ||
"import": "./../dev/rfc/index.mjs", | ||
"require": "./../dev/rfc/index.js" | ||
}, | ||
"default": { | ||
"import": "./../dist/rfc/index.mjs", | ||
"require": "./../dist/rfc/index.js" | ||
} | ||
} | ||
} |
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,31 @@ | ||
import React from 'react' | ||
|
||
type Slot = React.ElementType | ||
|
||
export const createSlotsManager = (onChange: (slot: Slot) => void) => { | ||
const elementMap = new Map<Slot, React.ReactElement>() | ||
return { | ||
register(slot: Slot, element: React.ReactElement) { | ||
elementMap.set(slot, element) | ||
}, | ||
update(slot: Slot, element: React.ReactElement) { | ||
elementMap.set(slot, element) | ||
onChange(slot) | ||
}, | ||
unmount(slot: Slot) { | ||
elementMap.delete(slot) | ||
onChange(slot) | ||
}, | ||
get<T extends Slot>(slot: T) { | ||
return elementMap.get(slot) as | ||
| React.ReactElement<React.ComponentProps<T>, T> | ||
| undefined | ||
}, | ||
getProps<T extends Slot>(slot: T) { | ||
const element = elementMap.get(slot) | ||
if (!element) return undefined | ||
const { ref, props } = element as any | ||
return (ref ? { ...props, ref } : props) as React.ComponentProps<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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as React from 'react' | ||
|
||
import { createSlotsContext, getComponentName } from '../utils' | ||
import { createSlotsManager } from './SlotsManager' | ||
|
||
type Slots = ReturnType<typeof createSlotsManager> | ||
type Callback = (Slots: Slots) => JSX.Element | null | ||
|
||
const SlotsContext = createSlotsContext<Slots | undefined>(undefined) | ||
|
||
export const SlotsHost = ({ | ||
children, | ||
callback, | ||
}: { | ||
children: React.ReactNode | ||
callback: Callback | ||
}) => { | ||
const forceUpdate = React.useReducer(() => [], [])[1] | ||
const Slots = React.useMemo( | ||
() => createSlotsManager(forceUpdate), | ||
[forceUpdate] | ||
) | ||
|
||
return ( | ||
<> | ||
<SlotsContext.Provider value={Slots}>{children}</SlotsContext.Provider> | ||
{callback(Slots)} | ||
</> | ||
) | ||
} | ||
|
||
export const createHost = (children: React.ReactNode, callback: Callback) => { | ||
return <SlotsHost children={children} callback={callback} /> | ||
} | ||
|
||
export const createSlot = <T extends React.ElementType>( | ||
Target: T, | ||
fallback?: boolean | ||
) => { | ||
const ForwardRef = (props: any, ref: any) => { | ||
const Slots = React.useContext(SlotsContext) | ||
if (!Slots) return fallback ? <Target ref={ref} {...props} /> : null | ||
|
||
const element = <Target ref={ref} {...props} /> | ||
React.useState(() => Slots.register(Slot, element)) | ||
React.useEffect(() => Slots.update(Slot, element)) | ||
React.useEffect(() => () => Slots.unmount(Slot), [Slots]) | ||
|
||
return null | ||
} | ||
ForwardRef.displayName = `Slot[${getComponentName(Target)}]` | ||
const Slot = React.forwardRef(ForwardRef) as unknown as T | ||
|
||
return Slot | ||
} |
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