Skip to content

Commit

Permalink
fix(accordion): variants for nested accordions (#3291)
Browse files Browse the repository at this point in the history
* feat(accordion): add data-variant

* fix(theme): revise accordion styles for variants

* feat(changeset): add changeset

* chore(theme): rollback content padding

* chore(accordion): abandon data-group approach

* refactor(theme): pass variant to accordionItem

* refactor(accordion): pass variant to accordionItem

* fix(accordion): revise accordion variants

* refactor(accordion): refine transitionVariants
  • Loading branch information
wingkwong authored Jun 22, 2024
1 parent 773f300 commit e9a6a16
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/nasty-forks-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/accordion": patch
"@nextui-org/theme": patch
---

Fixed variants for nested accordions (#3285)
15 changes: 11 additions & 4 deletions packages/components/accordion/src/accordion-item.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type {Variants} from "framer-motion";

import {forwardRef} from "@nextui-org/system";
import {useMemo, ReactNode} from "react";
import {ChevronIcon} from "@nextui-org/shared-icons";
Expand Down Expand Up @@ -53,15 +55,20 @@ const AccordionItem = forwardRef<"button", AccordionItemProps>((props, ref) => {
return <div {...getContentProps()}>{children}</div>;
}

const transitionVariants: Variants = {
exit: {...TRANSITION_VARIANTS.collapse.exit, overflowY: "hidden"},
enter: {...TRANSITION_VARIANTS.collapse.enter, overflowY: "unset"},
};

return keepContentMounted ? (
<LazyMotion features={domAnimation}>
<m.section
key="accordion-content"
animate={isOpen ? "enter" : "exit"}
exit="exit"
initial="exit"
style={{overflowY: "hidden", willChange}}
variants={TRANSITION_VARIANTS.collapse}
style={{willChange}}
variants={transitionVariants}
{...motionProps}
>
<div {...getContentProps()}>{children}</div>
Expand All @@ -76,8 +83,8 @@ const AccordionItem = forwardRef<"button", AccordionItemProps>((props, ref) => {
animate="enter"
exit="exit"
initial="exit"
style={{overflowY: "hidden", willChange}}
variants={TRANSITION_VARIANTS.collapse}
style={{willChange}}
variants={transitionVariants}
{...motionProps}
>
<div {...getContentProps()}>{children}</div>
Expand Down
1 change: 1 addition & 0 deletions packages/components/accordion/src/accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const AccordionGroup = forwardRef<"div", AccordionProps>((props, ref) => {
<Fragment key={item.key}>
<AccordionItem
item={item}
variant={props.variant}
onFocusChange={handleFocusChanged}
{...values}
{...item.props}
Expand Down
7 changes: 6 additions & 1 deletion packages/components/accordion/src/use-accordion-item.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type {AccordionItemVariantProps} from "@nextui-org/theme";

import {HTMLNextUIProps, PropGetter, useProviderContext} from "@nextui-org/system";
import {useFocusRing} from "@react-aria/focus";
import {accordionItem} from "@nextui-org/theme";
Expand Down Expand Up @@ -36,6 +38,7 @@ export interface Props<T extends object> extends HTMLNextUIProps<"div"> {
}

export type UseAccordionItemProps<T extends object = {}> = Props<T> &
AccordionItemVariantProps &
Omit<AccordionItemBaseProps, "onFocusChange">;

export function useAccordionItem<T extends object = {}>(props: UseAccordionItemProps<T>) {
Expand All @@ -53,6 +56,7 @@ export function useAccordionItem<T extends object = {}>(props: UseAccordionItemP
startContent,
motionProps,
focusedKey,
variant,
isCompact = false,
classNames: classNamesProp = {},
isDisabled: isDisabledProp = false,
Expand Down Expand Up @@ -125,8 +129,9 @@ export function useAccordionItem<T extends object = {}>(props: UseAccordionItemP
hideIndicator,
disableAnimation,
disableIndicatorAnimation,
variant,
}),
[isCompact, isDisabled, hideIndicator, disableAnimation, disableIndicatorAnimation],
[isCompact, isDisabled, hideIndicator, disableAnimation, disableIndicatorAnimation, variant],
);

const baseStyles = clsx(classNames?.base, className);
Expand Down
14 changes: 7 additions & 7 deletions packages/core/theme/src/components/accordion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const accordion = tv({
light: "",
shadow: "px-4 shadow-medium rounded-medium bg-content1",
bordered: "px-4 border-medium border-divider rounded-medium",
splitted: "group is-splitted flex flex-col gap-2", // the classNames are applied in the accordion-item component
splitted: "flex flex-col gap-2",
},
fullWidth: {
true: "w-full",
Expand Down Expand Up @@ -56,12 +56,7 @@ const accordion = tv({
*/
const accordionItem = tv({
slots: {
base: [
"group-[.is-splitted]:px-4",
"group-[.is-splitted]:bg-content1",
"group-[.is-splitted]:shadow-medium",
"group-[.is-splitted]:rounded-medium",
],
base: "",
heading: "",
trigger: [
"flex py-4 w-full h-full gap-3 outline-none items-center tap-highlight-transparent",
Expand All @@ -76,6 +71,11 @@ const accordionItem = tv({
content: "py-2",
},
variants: {
variant: {
splitted: {
base: "px-4 bg-content1 shadow-medium rounded-medium",
},
},
isCompact: {
true: {
trigger: "py-2",
Expand Down

0 comments on commit e9a6a16

Please sign in to comment.