Skip to content

Commit

Permalink
feat(Accordion): Added backgroundColor prop DEV-204
Browse files Browse the repository at this point in the history
  • Loading branch information
giubatt committed Jun 16, 2021
1 parent d41f7a5 commit 06521ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/components/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Icon from "../Icon/Icon"
import Divider from "../Divider/Divider"
import { AccordionContext, useAccordionContext } from "./context"
import { AnimatePresence, motion } from "framer-motion"
import { get } from "theme-ui"

const insertDivider = (items: Array<React.ReactNode>) => {
let result: Array<React.ReactNode> = []
Expand All @@ -26,9 +27,19 @@ const insertDivider = (items: Array<React.ReactNode>) => {
export interface AccordionProps {
initialOpenIndex?: number
children: React.ReactNode
activeBackgroundColor?:
| "primary"
| "secondary"
| "text"
| "success"
| "warning"
}

const Accordion = ({ children, initialOpenIndex }: AccordionProps) => {
const Accordion = ({
children,
initialOpenIndex,
activeBackgroundColor = "secondary",
}: AccordionProps) => {
const [openIndex, setOpenIndex] = useState<number | undefined>(
initialOpenIndex
)
Expand All @@ -45,7 +56,7 @@ const Accordion = ({ children, initialOpenIndex }: AccordionProps) => {
.filter((child) => React.isValidElement(child))
.map((child, index) => (
<AccordionContext.Provider
value={{ index, toggleOpen, openIndex }}
value={{ index, toggleOpen, openIndex, activeBackgroundColor }}
key={index}
>
{child}
Expand All @@ -62,11 +73,19 @@ export interface AccordionItemProps {
}

const AccordionItem = ({ title, children, className }: AccordionItemProps) => {
const { index, toggleOpen, openIndex } = useAccordionContext()
const { index, toggleOpen, openIndex, activeBackgroundColor } =
useAccordionContext()
const isOpen = openIndex === index
console.log({ activeBackgroundColor })

return (
<div>
<div
sx={{
backgroundColor: (t) =>
isOpen ? get(t, `colors.${activeBackgroundColor}.97`) : "transparent",
transition: "background-color 150ms linear",
}}
>
<div
role="button"
aria-disabled="false"
Expand Down
6 changes: 6 additions & 0 deletions src/components/Accordion/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export interface AccordionContext {
index: number
openIndex?: number
toggleOpen: (index: number) => void
activeBackgroundColor:
| "primary"
| "secondary"
| "text"
| "success"
| "warning"
}

export const AccordionContext = createContext<AccordionContext | null>(null)
Expand Down

0 comments on commit 06521ac

Please sign in to comment.