feat: add Disclosure DisclosureGroup and Accordion
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
provide,
|
||||
useAttrs,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import { accordionVariants, type AccordionVariants } from "@heroui-vue/styles";
|
||||
import { useInheritedDisabled } from "../field/disabled";
|
||||
import {
|
||||
disclosureGroupContextKey,
|
||||
useDisclosureGroupState,
|
||||
type DisclosureKey,
|
||||
} from "../disclosure-group/context";
|
||||
import { surfaceContextKey } from "../surface/context";
|
||||
import { provideAccordion } from "./context";
|
||||
|
||||
const DEFAULT_SURFACE_CONTEXT = { variant: computed(() => "default" as const) };
|
||||
|
||||
export default defineComponent({
|
||||
name: "Accordion",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
expandedKeys: {
|
||||
type: Array as PropType<DisclosureKey[] | undefined>,
|
||||
default: undefined,
|
||||
},
|
||||
defaultExpandedKeys: {
|
||||
type: Array as PropType<DisclosureKey[]>,
|
||||
default: () => [],
|
||||
},
|
||||
allowsMultipleExpanded: { type: Boolean, default: false },
|
||||
isDisabled: { type: Boolean, default: false },
|
||||
hideSeparator: { type: Boolean, default: false },
|
||||
variant: {
|
||||
type: String as PropType<NonNullable<AccordionVariants["variant"]>>,
|
||||
default: "default",
|
||||
},
|
||||
as: { type: String, default: "div" },
|
||||
},
|
||||
emits: ["update:expandedKeys", "expandedChange"],
|
||||
setup(props, { slots, emit, attrs }) {
|
||||
const inheritedDisabled = useInheritedDisabled(() => props.isDisabled);
|
||||
const state = useDisclosureGroupState({
|
||||
get expandedKeys() {
|
||||
return props.expandedKeys;
|
||||
},
|
||||
get defaultExpandedKeys() {
|
||||
return props.defaultExpandedKeys;
|
||||
},
|
||||
get allowsMultipleExpanded() {
|
||||
return props.allowsMultipleExpanded;
|
||||
},
|
||||
get isDisabled() {
|
||||
return inheritedDisabled.value;
|
||||
},
|
||||
onExpandedChange(keys) {
|
||||
emit("update:expandedKeys", keys);
|
||||
emit("expandedChange", keys);
|
||||
},
|
||||
});
|
||||
|
||||
provide(disclosureGroupContextKey, {
|
||||
expandedKeys: state.expandedKeys,
|
||||
allowsMultipleExpanded: state.allowsMultipleExpanded,
|
||||
isDisabled: computed(() => inheritedDisabled.value),
|
||||
isExpanded: state.isExpanded,
|
||||
toggle: state.toggle,
|
||||
registerItem: state.registerItem,
|
||||
itemKeys: state.itemKeys,
|
||||
});
|
||||
|
||||
provideAccordion({
|
||||
hideSeparator: computed(() => props.hideSeparator),
|
||||
variant: computed(() => props.variant),
|
||||
});
|
||||
|
||||
if (props.variant === "surface") {
|
||||
provide(surfaceContextKey, DEFAULT_SURFACE_CONTEXT);
|
||||
}
|
||||
|
||||
const slotClasses = computed(() =>
|
||||
accordionVariants({ variant: props.variant }),
|
||||
);
|
||||
|
||||
return () => {
|
||||
const raw =
|
||||
slots.default?.({
|
||||
expandedKeys: state.expandedKeys.value,
|
||||
isDisabled: inheritedDisabled.value,
|
||||
}) ?? [];
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
class: [slotClasses.value.base(), attrs.class],
|
||||
"data-slot": "accordion",
|
||||
"data-disabled": inheritedDisabled.value ? "true" : undefined,
|
||||
"aria-disabled": inheritedDisabled.value ? true : undefined,
|
||||
},
|
||||
raw,
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,30 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, useAttrs } from "vue";
|
||||
import { accordionVariants } from "@heroui-vue/styles";
|
||||
import { injectAccordion } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(defineProps<{ as?: string }>(), { as: "div" });
|
||||
const attrs = useAttrs();
|
||||
const accordion = injectAccordion();
|
||||
const slotClasses = computed(() =>
|
||||
accordionVariants({ variant: accordion?.variant.value }),
|
||||
);
|
||||
/** Baseline: className merges onto the inner wrapper; other attrs stay on the outer. */
|
||||
const outerAttrs = computed(() => {
|
||||
const { class: _c, ...rest } = attrs;
|
||||
return rest;
|
||||
});
|
||||
const innerClass = computed(() => [slotClasses.value.bodyInner(), attrs.class]);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<component
|
||||
:is="props.as"
|
||||
v-bind="outerAttrs"
|
||||
:class="slotClasses.body()"
|
||||
data-slot="accordion-body"
|
||||
>
|
||||
<div :class="innerClass"><slot /></div>
|
||||
</component>
|
||||
</template>
|
||||
@@ -0,0 +1,33 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, inject, useAttrs } from "vue";
|
||||
import { accordionVariants } from "@heroui-vue/styles";
|
||||
import { disclosureItemContextKey } from "../disclosure-group/context";
|
||||
import { injectAccordion } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const props = withDefaults(defineProps<{ as?: string; level?: number }>(), {
|
||||
as: "h3",
|
||||
level: 3,
|
||||
});
|
||||
const attrs = useAttrs();
|
||||
const accordion = injectAccordion();
|
||||
const item = inject(disclosureItemContextKey, undefined);
|
||||
const slotClasses = computed(() =>
|
||||
accordionVariants({ variant: accordion?.variant.value }),
|
||||
);
|
||||
const tag = computed(() =>
|
||||
props.as && props.as !== "h3" ? props.as : `h${props.level ?? 3}`,
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<component
|
||||
:is="tag"
|
||||
v-bind="attrs"
|
||||
:class="[slotClasses.heading(), attrs.class]"
|
||||
data-slot="accordion-heading"
|
||||
>
|
||||
<slot v-bind="item ? { isExpanded: item.isExpanded.value } : {}" />
|
||||
</component>
|
||||
</template>
|
||||
@@ -0,0 +1,59 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
cloneVNode,
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
isVNode,
|
||||
useAttrs,
|
||||
type VNode,
|
||||
} from "vue";
|
||||
import { accordionVariants } from "@heroui-vue/styles";
|
||||
import { disclosureItemContextKey } from "../disclosure-group/context";
|
||||
import DisclosureChevronIcon from "../disclosure/DisclosureChevronIcon.vue";
|
||||
import { injectAccordion } from "./context";
|
||||
|
||||
function firstVNode(list: unknown[]): VNode | undefined {
|
||||
for (const node of list) {
|
||||
if (Array.isArray(node)) {
|
||||
const found = firstVNode(node);
|
||||
if (found) return found;
|
||||
} else if (isVNode(node)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: "AccordionIndicator",
|
||||
inheritAttrs: false,
|
||||
setup(_, { slots, attrs }) {
|
||||
const item = inject(disclosureItemContextKey, undefined);
|
||||
const accordion = injectAccordion();
|
||||
const isExpanded = computed(() => item?.isExpanded.value === true);
|
||||
const slotClasses = computed(() =>
|
||||
accordionVariants({ variant: accordion?.variant.value }),
|
||||
);
|
||||
|
||||
return () => {
|
||||
const children = slots.default?.({ isExpanded: isExpanded.value });
|
||||
const shared = {
|
||||
...attrs,
|
||||
class: [slotClasses.value.indicator(), attrs.class],
|
||||
"data-expanded": isExpanded.value ? "true" : undefined,
|
||||
"data-slot": "accordion-indicator",
|
||||
};
|
||||
const child = firstVNode(Array.isArray(children) ? children : [children]);
|
||||
if (child) {
|
||||
return cloneVNode(child, {
|
||||
...shared,
|
||||
class: [shared.class, child.props?.class],
|
||||
});
|
||||
}
|
||||
return h(DisclosureChevronIcon, shared);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,74 @@
|
||||
<script lang="ts">
|
||||
import { computed, defineComponent, h, useAttrs, type PropType } from "vue";
|
||||
import { accordionVariants } from "@heroui-vue/styles";
|
||||
import type { DisclosureKey } from "../disclosure-group/context";
|
||||
import { useDisclosureItem } from "../disclosure/useDisclosureItem";
|
||||
import { injectAccordion } from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "AccordionItem",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
/** Group key (type-preserving). DOM ids stay unique when keys stringify alike. */
|
||||
id: {
|
||||
type: [String, Number] as PropType<DisclosureKey>,
|
||||
default: undefined,
|
||||
},
|
||||
expanded: {
|
||||
type: Boolean as PropType<boolean | undefined>,
|
||||
default: undefined,
|
||||
},
|
||||
defaultExpanded: { type: Boolean, default: false },
|
||||
isDisabled: { type: Boolean, default: false },
|
||||
as: { type: String, default: "div" },
|
||||
},
|
||||
emits: ["update:expanded", "expandedChange"],
|
||||
setup(props, { slots, emit, attrs }) {
|
||||
const accordion = injectAccordion();
|
||||
const item = useDisclosureItem({
|
||||
get id() {
|
||||
return props.id;
|
||||
},
|
||||
get expanded() {
|
||||
return props.expanded;
|
||||
},
|
||||
get defaultExpanded() {
|
||||
return props.defaultExpanded;
|
||||
},
|
||||
get isDisabled() {
|
||||
return props.isDisabled;
|
||||
},
|
||||
onExpanded(next) {
|
||||
emit("update:expanded", next);
|
||||
emit("expandedChange", next);
|
||||
},
|
||||
});
|
||||
|
||||
const slotClasses = computed(() =>
|
||||
accordionVariants({ variant: accordion?.variant.value }),
|
||||
);
|
||||
|
||||
return () => {
|
||||
const raw =
|
||||
slots.default?.({
|
||||
isExpanded: item.isExpanded.value,
|
||||
isDisabled: item.isDisabled.value,
|
||||
}) ?? [];
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
id: item.domId.value,
|
||||
class: [slotClasses.value.item(), attrs.class],
|
||||
"data-slot": "accordion-item",
|
||||
"data-expanded": item.isExpanded.value ? "true" : undefined,
|
||||
"data-disabled": item.isDisabled.value ? "true" : undefined,
|
||||
"data-hide-separator":
|
||||
accordion?.hideSeparator.value === true ? "true" : undefined,
|
||||
},
|
||||
raw,
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,45 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, inject, ref, useAttrs } from "vue";
|
||||
import { accordionVariants } from "@heroui-vue/styles";
|
||||
import { disclosureItemContextKey } from "../disclosure-group/context";
|
||||
import { useDisclosurePanelHeight } from "../disclosure/useDisclosurePanelHeight";
|
||||
import { injectAccordion } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const props = withDefaults(defineProps<{ as?: string }>(), { as: "div" });
|
||||
|
||||
const attrs = useAttrs();
|
||||
const item = inject(disclosureItemContextKey, undefined);
|
||||
const accordion = injectAccordion();
|
||||
const isExpanded = computed(() => item?.isExpanded.value === true);
|
||||
const isDisabled = computed(() => item?.isDisabled.value === true);
|
||||
const contentId = computed(() => item?.contentId.value);
|
||||
const triggerId = computed(() => item?.triggerId.value);
|
||||
|
||||
const contentEl = ref<HTMLElement | null>(null);
|
||||
const { hidden } = useDisclosurePanelHeight(contentEl, isExpanded);
|
||||
|
||||
const slotClasses = computed(() =>
|
||||
accordionVariants({ variant: accordion?.variant.value }),
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<component
|
||||
:is="props.as"
|
||||
:id="contentId"
|
||||
v-bind="attrs"
|
||||
ref="contentEl"
|
||||
:class="[slotClasses.panel(), attrs.class]"
|
||||
:data-expanded="isExpanded ? 'true' : undefined"
|
||||
:data-disabled="isDisabled ? 'true' : undefined"
|
||||
:hidden="hidden"
|
||||
:aria-hidden="!isExpanded"
|
||||
:aria-labelledby="triggerId"
|
||||
:role="(attrs.role as string | undefined) ?? 'group'"
|
||||
data-slot="accordion-panel"
|
||||
>
|
||||
<slot v-bind="{ isExpanded, isDisabled }" />
|
||||
</component>
|
||||
</template>
|
||||
@@ -0,0 +1,94 @@
|
||||
<script lang="ts">
|
||||
import { computed, defineComponent, h, inject, useAttrs } from "vue";
|
||||
import { accordionVariants } from "@heroui-vue/styles";
|
||||
import { useInheritedDisabled } from "../field/disabled";
|
||||
import { disclosureItemContextKey } from "../disclosure-group/context";
|
||||
import {
|
||||
cloneAsChildRoot,
|
||||
resolveAsChildTarget,
|
||||
} from "../disclosure/triggerEvents";
|
||||
import { injectAccordion } from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "AccordionTrigger",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
as: { type: String, default: "button" },
|
||||
asChild: { type: Boolean, default: false },
|
||||
isDisabled: { type: Boolean, default: false },
|
||||
},
|
||||
emits: ["click"],
|
||||
setup(props, { slots, emit, attrs }) {
|
||||
const item = inject(disclosureItemContextKey, undefined);
|
||||
const accordion = injectAccordion();
|
||||
const localDisabled = useInheritedDisabled(() => props.isDisabled);
|
||||
const isDisabled = computed(
|
||||
() => localDisabled.value || item?.isDisabled.value === true,
|
||||
);
|
||||
const isExpanded = computed(() => item?.isExpanded.value === true);
|
||||
const slotClasses = computed(() =>
|
||||
accordionVariants({ variant: accordion?.variant.value }),
|
||||
);
|
||||
|
||||
return () => {
|
||||
const children = slots.default?.({
|
||||
isExpanded: isExpanded.value,
|
||||
isDisabled: isDisabled.value,
|
||||
});
|
||||
const shared: Record<string, unknown> = {
|
||||
...attrs,
|
||||
type: props.as === "button" ? "button" : undefined,
|
||||
"aria-expanded": isExpanded.value,
|
||||
"aria-controls": item?.contentId.value,
|
||||
id: (attrs.id as string | undefined) ?? item?.triggerId.value,
|
||||
disabled: isDisabled.value ? true : undefined,
|
||||
"aria-disabled": isDisabled.value ? true : undefined,
|
||||
"data-disabled": isDisabled.value ? "true" : undefined,
|
||||
"data-slot": "accordion-trigger",
|
||||
};
|
||||
|
||||
if (props.asChild) {
|
||||
const target = resolveAsChildTarget(children);
|
||||
if (!target.root) {
|
||||
if (target.nodes.length > 1) {
|
||||
console.warn(
|
||||
"[heroui-vue] AccordionTrigger asChild expects a single effective root; rendering slot unchanged.",
|
||||
);
|
||||
}
|
||||
return target.fallback;
|
||||
}
|
||||
return cloneAsChildRoot(target.root, {
|
||||
isDisabled: () => isDisabled.value,
|
||||
emitClick: (event) => emit("click", event),
|
||||
activate: () => item?.toggle(),
|
||||
class: [
|
||||
slotClasses.value.trigger(),
|
||||
attrs.class,
|
||||
target.root.props?.class,
|
||||
],
|
||||
shared,
|
||||
});
|
||||
}
|
||||
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
...shared,
|
||||
onClick: (event: MouseEvent) => {
|
||||
if (isDisabled.value) {
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
return;
|
||||
}
|
||||
emit("click", event);
|
||||
if (event.defaultPrevented) return;
|
||||
item?.toggle();
|
||||
},
|
||||
class: [slotClasses.value.trigger(), attrs.class],
|
||||
},
|
||||
children,
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,24 @@
|
||||
import {
|
||||
computed,
|
||||
inject,
|
||||
provide,
|
||||
type ComputedRef,
|
||||
type InjectionKey,
|
||||
} from "vue";
|
||||
import type { AccordionVariants } from "@heroui-vue/styles";
|
||||
|
||||
export interface AccordionContext {
|
||||
hideSeparator: ComputedRef<boolean>;
|
||||
variant: ComputedRef<NonNullable<AccordionVariants["variant"]>>;
|
||||
}
|
||||
|
||||
export const accordionContextKey: InjectionKey<AccordionContext> =
|
||||
Symbol("hv-accordion");
|
||||
|
||||
export function provideAccordion(ctx: AccordionContext): void {
|
||||
provide(accordionContextKey, ctx);
|
||||
}
|
||||
|
||||
export function injectAccordion(): AccordionContext | undefined {
|
||||
return inject(accordionContextKey, undefined);
|
||||
}
|
||||
Reference in New Issue
Block a user