-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
2 changed files
with
127 additions
and
0 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,115 @@ | ||
import { | ||
createContext, | ||
createEffect, | ||
createSignal, | ||
JSX, | ||
untrack, | ||
useContext, | ||
} from 'solid-js'; | ||
|
||
export interface HeadlessToggleOptions { | ||
checked?: boolean; | ||
defaultChecked?: boolean; | ||
disabled?: boolean; | ||
onChange?: (state?: boolean) => void; | ||
} | ||
|
||
export interface HeadlessToggleProperties { | ||
checked(): boolean | undefined; | ||
setState(newState?: boolean): void; | ||
disabled(): boolean; | ||
} | ||
|
||
export function useHeadlessToggle( | ||
options: HeadlessToggleOptions = {}, | ||
): HeadlessToggleProperties { | ||
const [signal, setSignal] = createSignal(untrack(() => options.defaultChecked)); | ||
|
||
let initial = true; | ||
|
||
createEffect(() => { | ||
const value = options.checked; | ||
if (initial) { | ||
initial = false; | ||
} else { | ||
setSignal(!!value); | ||
} | ||
}); | ||
|
||
return { | ||
checked() { | ||
return signal(); | ||
}, | ||
setState(value) { | ||
if (!options.disabled) { | ||
setSignal(value); | ||
options.onChange?.(value); | ||
} | ||
}, | ||
disabled() { | ||
return !!options.disabled; | ||
}, | ||
}; | ||
} | ||
|
||
const HeadlessToggleContext = createContext<HeadlessToggleProperties>(); | ||
|
||
export type HeadlessToggleRootRenderProp = ( | ||
(properties: HeadlessToggleProperties) => JSX.Element | ||
); | ||
|
||
function isHeadlessToggleRootRenderProp( | ||
children: HeadlessToggleRootRenderProp | JSX.Element, | ||
): children is HeadlessToggleRootRenderProp { | ||
return typeof children === 'function' && children.length > 0; | ||
} | ||
|
||
export interface HeadlessToggleRootProps extends HeadlessToggleOptions { | ||
children?: HeadlessToggleRootRenderProp | JSX.Element; | ||
} | ||
|
||
export function HeadlessToggleRoot(props: HeadlessToggleRootProps): JSX.Element { | ||
const properties = useHeadlessToggle(props); | ||
return ( | ||
<HeadlessToggleContext.Provider value={properties}> | ||
{(() => { | ||
const body = props.children; | ||
if (isHeadlessToggleRootRenderProp(body)) { | ||
return body(properties); | ||
} | ||
return body; | ||
})()} | ||
</HeadlessToggleContext.Provider> | ||
); | ||
} | ||
|
||
export function useHeadlessToggleChild(): HeadlessToggleProperties { | ||
const properties = useContext(HeadlessToggleContext); | ||
if (properties) { | ||
return properties; | ||
} | ||
throw new Error('`useToggleChild` must be used within ToggleRoot.'); | ||
} | ||
|
||
export type HeadlessToggleChildRenderProp = ( | ||
(properties: HeadlessToggleProperties) => JSX.Element | ||
); | ||
|
||
function isHeadlessToggleChildRenderProp( | ||
children: HeadlessToggleChildRenderProp | JSX.Element, | ||
): children is HeadlessToggleChildRenderProp { | ||
return typeof children === 'function' && children.length > 0; | ||
} | ||
|
||
export interface HeadlessToggleChildProps { | ||
children?: HeadlessToggleChildRenderProp | JSX.Element; | ||
} | ||
|
||
export function HeadlessToggleChild(props: HeadlessToggleChildProps): JSX.Element { | ||
const properties = useHeadlessToggleChild(); | ||
const body = props.children; | ||
if (isHeadlessToggleChildRenderProp(body)) { | ||
return body(properties); | ||
} | ||
return body; | ||
} |
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