-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
docs(changeset): improve SegmentedControl
1 parent
ef33eb7
commit 61d8687
Showing
5 changed files
with
89 additions
and
67 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,5 @@ | ||
--- | ||
"@codeui/kit": patch | ||
--- | ||
|
||
improve SegmentedControl |
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
35 changes: 35 additions & 0 deletions
35
packages/kit/src/components/SegmentedControl/SegmentedControlContext.tsx
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,35 @@ | ||
import { Accessor, createContext, createSignal, useContext } from "solid-js"; | ||
|
||
export interface SegmentedControlContextState { | ||
items: Accessor<HTMLElement[]>; | ||
|
||
mountItem(el: HTMLElement): void; | ||
|
||
unmountItem(el: HTMLElement): void; | ||
} | ||
|
||
export const SegmentedControlContext = createContext<SegmentedControlContextState>(); | ||
|
||
export function useSegmentedControlContext() { | ||
const context = useContext(SegmentedControlContext); | ||
if (context === undefined) { | ||
throw new Error( | ||
"[@codeui/kit]: `useSegmentedControlContext` must be used within a `SegmentedControl` component", | ||
); | ||
} | ||
return context; | ||
} | ||
|
||
export function createSegmentedControlContextState() { | ||
const [items, setItems] = createSignal<HTMLElement[]>([]); | ||
const context: SegmentedControlContextState = { | ||
items, | ||
mountItem(el) { | ||
setItems(items => items.concat(el)); | ||
}, | ||
unmountItem(el) { | ||
setItems(items => items.filter(item => item !== el)); | ||
}, | ||
}; | ||
return context; | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/kit/src/components/SegmentedControl/SegmentedControlItem.tsx
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,30 @@ | ||
import { onCleanup, onMount, splitProps } from "solid-js"; | ||
import { mergeClasses } from "../../utils/css"; | ||
import * as styles from "./SegmentedControl.css"; | ||
import { Tabs } from "@kobalte/core"; | ||
import { mergeRefs } from "@kobalte/utils"; | ||
import { useSegmentedControlContext } from "./SegmentedControlContext"; | ||
|
||
export type SegmentedControlItemProps = Tabs.TabsTriggerProps; | ||
|
||
export function SegmentedControlItem(props: SegmentedControlItemProps) { | ||
const [local, others] = splitProps(props, ["class"]); | ||
const context = useSegmentedControlContext(); | ||
let ref: HTMLElement; | ||
|
||
const classes = () => mergeClasses(styles.segment, local.class); | ||
|
||
onMount(() => { | ||
context.mountItem(ref); | ||
onCleanup(() => context.unmountItem(ref)); | ||
}); | ||
|
||
return ( | ||
<Tabs.Trigger | ||
data-cui={"segmentedControlItem"} | ||
class={classes()} | ||
ref={mergeRefs(props.ref, el => (ref = el))} | ||
{...others} | ||
/> | ||
); | ||
} |
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