-
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
15 changed files
with
500 additions
and
30 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,37 @@ | ||
import React, { useId } from 'react' | ||
import { createHost, createSlot } from 'create-slots/rfc' | ||
|
||
type ItemProps = Omit<React.ComponentPropsWithoutRef<'li'>, 'value'> & { | ||
value: string | ||
} | ||
|
||
const ItemTitle = createSlot<'h4'>() | ||
const ItemDescription = createSlot<'div'>() | ||
|
||
export const Item = (props: ItemProps) => { | ||
const id = useId() | ||
|
||
return createHost(props.children, (slots) => { | ||
const titleSlot = slots.find((slot) => slot.type === ItemTitle) | ||
const descriptionSlot = slots.find((slot) => slot.type === ItemDescription) | ||
const titleId = titleSlot ? `${id}-title` : undefined | ||
const descId = descriptionSlot ? `${id}-desc` : undefined | ||
return ( | ||
<li aria-describedby={descId} aria-label={titleId} {...props}> | ||
{titleSlot && ( | ||
<h4 id={titleId} ref={(titleSlot as any).ref} {...titleSlot.props} /> | ||
)} | ||
{descriptionSlot && ( | ||
<div | ||
id={descId} | ||
ref={(descriptionSlot as any).ref} | ||
{...descriptionSlot.props} | ||
/> | ||
)} | ||
</li> | ||
) | ||
}) | ||
} | ||
|
||
Item.Title = ItemTitle | ||
Item.Description = ItemDescription |
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,59 @@ | ||
import React, { useState } from 'react' | ||
import { createHost, createSlot } from 'create-slots/rfc' | ||
|
||
import { Item } from './RFCItem' | ||
|
||
const SelectItem = createSlot<typeof Item>() | ||
const SelectDivider = createSlot<'hr'>() | ||
|
||
export const Select = (props: React.ComponentProps<'ul'>) => { | ||
const [selected, setSelected] = useState<React.ReactNode>(null) | ||
|
||
return ( | ||
<div> | ||
<div>Selected: {selected}</div> | ||
{createHost(props.children, (slots) => { | ||
let index = 0 | ||
return ( | ||
<ul {...props}> | ||
{slots.map((slot) => { | ||
if (slot.type === SelectItem) { | ||
const itemProps = slot.props | ||
|
||
return ( | ||
<Item | ||
key={slot.key} | ||
{...slot.props} | ||
{...{ | ||
role: 'button', | ||
tabIndex: 0, | ||
'data-index': index, | ||
'aria-selected': itemProps.children === selected, | ||
onClick: () => { | ||
setSelected(itemProps.value) | ||
}, | ||
onKeyDown: ( | ||
event: React.KeyboardEvent<HTMLButtonElement> | ||
) => { | ||
if (event.key === 'Enter' || event.key === ' ') { | ||
setSelected(itemProps.value) | ||
} | ||
}, | ||
}} | ||
/> | ||
) | ||
} | ||
return <hr key={slot.key} {...slot.props} /> | ||
})} | ||
</ul> | ||
) | ||
})} | ||
</div> | ||
) | ||
} | ||
|
||
Select.Item = SelectItem | ||
Select.Divider = SelectDivider | ||
|
||
Select.Item.Title = Item.Title | ||
Select.Item.Description = Item.Description |
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/simple' | ||
|
||
const Description = (props: React.ComponentPropsWithoutRef<'div'>) => ( | ||
<div | ||
{...props} | ||
style={{ borderLeft: '4px solid lightgray', paddingLeft: 4 }} | ||
/> | ||
) | ||
|
||
const FieldLabel = createSlot('label') | ||
const FieldInput = createSlot('input') | ||
const FieldDescription = createSlot(Description) | ||
|
||
const StyledLabel = (props: React.ComponentPropsWithoutRef<'label'>) => ( | ||
<FieldLabel {...props} style={{ color: 'red' }} /> | ||
) | ||
|
||
export const Field = (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(FieldLabel) | ||
const inputProps = Slots.getProps(FieldInput) | ||
const descriptionIdProps = Slots.getProps(FieldDescription) | ||
|
||
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> | ||
) | ||
} | ||
|
||
Field.Label = FieldLabel | ||
Field.Input = FieldInput | ||
Field.Description = FieldDescription | ||
Field.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
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,15 @@ | ||
{ | ||
"main": "../dist/simple/index.js", | ||
"module": "../dist/simple/index.mjs", | ||
"types": "../dist/simple/index.d.ts", | ||
"exports": { | ||
"development": { | ||
"import": "./../dev/simple/index.mjs", | ||
"require": "./../dev/simple/index.js" | ||
}, | ||
"default": { | ||
"import": "./../dist/simple/index.mjs", | ||
"require": "./../dist/simple/index.js" | ||
} | ||
} | ||
} |
File renamed without changes.
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
Oops, something went wrong.