-
-
Notifications
You must be signed in to change notification settings - Fork 336
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
10 changed files
with
195 additions
and
51 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,11 +1,21 @@ | ||
<script lang="ts"> | ||
import { Accordion as AccordionPrimitive } from "radix-svelte"; | ||
import { setAccordion, melt, type CreateAccordionProps } from "."; | ||
// Remove the any when radix-svelte is updated to properly satisfy the types | ||
// https://www.radix-svelte.com/docs/accordion | ||
export let value: any = undefined; | ||
export let multiple: CreateAccordionProps["multiple"] = false; | ||
export let disabled: CreateAccordionProps["disabled"] = false; | ||
export let defaultValue: CreateAccordionProps["defaultValue"] = undefined; | ||
export let value: CreateAccordionProps["value"] = undefined; | ||
export let onValueChange: CreateAccordionProps["onValueChange"] = undefined; | ||
const root = setAccordion({ | ||
value, | ||
multiple, | ||
disabled, | ||
defaultValue, | ||
onValueChange | ||
}); | ||
</script> | ||
|
||
<AccordionPrimitive.Root bind:value {...$$restProps}> | ||
<div use:melt={$root} {...$$restProps}> | ||
<slot /> | ||
</AccordionPrimitive.Root> | ||
</div> |
24 changes: 13 additions & 11 deletions
24
apps/www/src/lib/components/ui/accordion/AccordionContent.svelte
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 |
---|---|---|
@@ -1,19 +1,21 @@ | ||
<script lang="ts"> | ||
import type { AccordionContentProps } from "radix-svelte"; | ||
import { Accordion as AccordionPrimitive } from "radix-svelte"; | ||
import { cn } from "$lib/utils"; | ||
import { getContent, melt } from "."; | ||
let className: string | undefined | null = undefined; | ||
export { className as class }; | ||
export let transition: AccordionContentProps["transition"] = true; | ||
const { content, isSelected, props } = getContent(); | ||
</script> | ||
|
||
<AccordionPrimitive.Content | ||
class={cn("overflow-hidden text-sm", className)} | ||
{...$$restProps} | ||
{transition} | ||
> | ||
<div class="pb-4 pt-0"> | ||
<slot /> | ||
{#if $isSelected(props)} | ||
<div | ||
use:melt={$content(props)} | ||
class={cn("overflow-hidden text-sm", className)} | ||
{...$$restProps} | ||
> | ||
<div class="pb-4 pt-0"> | ||
<slot /> | ||
</div> | ||
</div> | ||
</AccordionPrimitive.Content> | ||
{/if} |
14 changes: 6 additions & 8 deletions
14
apps/www/src/lib/components/ui/accordion/AccordionItem.svelte
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 |
---|---|---|
@@ -1,17 +1,15 @@ | ||
<script lang="ts"> | ||
import type { AccordionItemProps } from "radix-svelte"; | ||
import { Accordion as AccordionPrimitive } from "radix-svelte"; | ||
import { cn } from "$lib/utils"; | ||
import { setAccordionItem, type AccordionItemProps, melt } from "."; | ||
let className: string | undefined | null = undefined; | ||
export { className as class }; | ||
export let value: AccordionItemProps["value"]; | ||
export let disabled: AccordionItemProps["disabled"] = false; | ||
const { item, props } = setAccordionItem({ value, disabled }); | ||
</script> | ||
|
||
<AccordionPrimitive.Item | ||
{value} | ||
class={cn("border-b", className)} | ||
{...$$restProps} | ||
> | ||
<div use:melt={$item(props)} class={cn("border-b", className)} {...$$restProps}> | ||
<slot /> | ||
</AccordionPrimitive.Item> | ||
</div> |
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 |
---|---|---|
@@ -1,4 +1,81 @@ | ||
import { | ||
createAccordion, | ||
type Accordion, | ||
type AccordionItemProps as ItemProps, | ||
type CreateAccordionProps, | ||
type AccordionHeadingProps as HeadingProps | ||
} from "@melt-ui/svelte"; | ||
import { getContext, setContext } from "svelte"; | ||
|
||
export { default as Accordion } from "./Accordion.svelte"; | ||
export { default as AccordionContent } from "./AccordionContent.svelte"; | ||
export { default as AccordionItem } from "./AccordionItem.svelte"; | ||
export { default as AccordionTrigger } from "./AccordionTrigger.svelte"; | ||
export { melt, type CreateAccordionProps } from "@melt-ui/svelte"; | ||
|
||
type ObjectVariation<T> = T extends object ? T : never; | ||
export type AccordionItemProps = ObjectVariation<ItemProps>; | ||
export type AccordionTriggerProps = AccordionItemProps; | ||
export type AccordionHeadingProps = ObjectVariation<HeadingProps>; | ||
|
||
export type AccordionElements = { | ||
item: AccordionItemProps; | ||
content: AccordionItemProps; | ||
trigger: AccordionTriggerProps; | ||
heading: AccordionHeadingProps; | ||
}; | ||
|
||
const ITEM_PROPS = "accordion_itemProps"; | ||
const COMPONENT_NAME = "accordion"; | ||
|
||
export function setAccordion(props: CreateAccordionProps) { | ||
setContext(COMPONENT_NAME, createAccordion({ ...props })); | ||
const { | ||
elements: { root } | ||
} = getContext<Accordion>(COMPONENT_NAME); | ||
return root; | ||
} | ||
|
||
export function getAccordion() { | ||
return getContext<Accordion>(COMPONENT_NAME); | ||
} | ||
|
||
export function setAccordionItem(props: AccordionItemProps) { | ||
setContext(ITEM_PROPS, props); | ||
const { | ||
elements: { item } | ||
} = getAccordion(); | ||
return { item, props: getContext<AccordionItemProps>(ITEM_PROPS) }; | ||
} | ||
|
||
export function getItemProps() { | ||
const itemProps = getContext<AccordionItemProps>(ITEM_PROPS); | ||
return itemProps; | ||
} | ||
|
||
export function getContent() { | ||
const { | ||
elements: { content }, | ||
helpers: { isSelected }, | ||
states: { value } | ||
} = getAccordion(); | ||
const { value: props } = getItemProps(); | ||
return { content, props, isSelected, value }; | ||
} | ||
|
||
export function getTriggerAndHeading(level: AccordionHeadingProps["level"]) { | ||
const { | ||
elements: { trigger, heading }, | ||
states: { value } | ||
} = getAccordion(); | ||
const props = getItemProps(); | ||
return { | ||
trigger, | ||
heading, | ||
props: { | ||
heading: { level }, | ||
trigger: props | ||
}, | ||
value | ||
}; | ||
} |
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.