Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion public/registry/accordion.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,28 @@
{
"name": "Accordion.astro",
"path": "accordion/Accordion.astro",
"content": "---\nimport { cn } from '@/utils/cn';\nimport type { HTMLAttributes } from 'astro/types';\n\nexport interface Props extends HTMLAttributes<'div'> {\n type?: 'single' | 'multiple';\n}\n\nconst {\n type = 'single',\n class: className,\n ...props\n} = Astro.props;\n\nconst classes = cn('w-full', className);\n---\n\n<div class={classes} data-accordion data-type={type} {...props}>\n <slot />\n</div>\n\n<script>\n import { generateId } from '@/utils/focus-trap';\n\n const running = new WeakMap<HTMLElement, Animation>();\n\n function prefersReducedMotion() {\n return window.matchMedia('(prefers-reduced-motion: reduce)').matches;\n }\n\n function openContent(content: HTMLElement) {\n // If an animation is in flight, continue from its current height\n const prev = running.get(content);\n const startHeight = prev ? content.getBoundingClientRect().height : 0;\n prev?.cancel();\n content.hidden = false;\n content.setAttribute('data-state', 'open');\n if (prefersReducedMotion()) return;\n const anim = content.animate(\n [{ height: `${startHeight}px` }, { height: `${content.scrollHeight}px` }],\n { duration: 200, easing: 'ease-out' },\n );\n running.set(content, anim);\n }\n\n function closeContent(content: HTMLElement) {\n const prev = running.get(content);\n const startHeight = prev\n ? content.getBoundingClientRect().height\n : content.scrollHeight;\n prev?.cancel();\n content.setAttribute('data-state', 'closed');\n if (prefersReducedMotion()) {\n content.hidden = true;\n return;\n }\n const anim = content.animate(\n [{ height: `${startHeight}px` }, { height: '0px' }],\n { duration: 200, easing: 'ease-out' },\n );\n anim.onfinish = () => {\n content.hidden = true;\n };\n running.set(content, anim);\n }\n\n function initAccordions() {\n document.querySelectorAll('[data-accordion]').forEach((accordion) => {\n if (accordion.hasAttribute('data-initialized')) return;\n accordion.setAttribute('data-initialized', 'true');\n\n const type = accordion.getAttribute('data-type');\n \n // Only select direct accordion items (not nested ones)\n const items = Array.from(accordion.querySelectorAll('[data-accordion-item]')).filter(\n (item) => item.closest('[data-accordion]') === accordion\n );\n\n items.forEach((item) => {\n const trigger = item.querySelector(':scope > [data-accordion-trigger]') as HTMLElement;\n const content = item.querySelector(':scope > [data-accordion-content]') as HTMLElement;\n\n if (!trigger || !content) return;\n\n // Generate IDs and set aria-controls\n if (!content.id) {\n content.id = generateId('accordion-content');\n }\n if (!trigger.id) {\n trigger.id = generateId('accordion-trigger');\n }\n\n trigger.setAttribute('aria-controls', content.id);\n content.setAttribute('aria-labelledby', trigger.id);\n });\n\n // Only select triggers that belong directly to this accordion\n const triggers = Array.from(accordion.querySelectorAll('[data-accordion-trigger]')).filter(\n (trigger) => trigger.closest('[data-accordion]') === accordion\n ) as HTMLElement[];\n\n triggers.forEach((trigger) => {\n trigger.addEventListener('click', (e) => {\n // Prevent event from bubbling to parent accordions\n e.stopPropagation();\n \n const item = trigger.closest('[data-accordion-item]');\n const content = item?.querySelector(':scope > [data-accordion-content]') as HTMLElement;\n const isOpen = trigger.getAttribute('data-state') === 'open';\n\n if (type === 'single') {\n // Close all other items in THIS accordion only\n triggers.forEach((t) => {\n if (t !== trigger) {\n t.setAttribute('data-state', 'closed');\n t.setAttribute('aria-expanded', 'false');\n const otherItem = t.closest('[data-accordion-item]');\n const otherContent = otherItem?.querySelector(':scope > [data-accordion-content]') as HTMLElement;\n if (otherContent && !otherContent.hidden) {\n closeContent(otherContent);\n }\n }\n });\n }\n\n // Toggle current item\n const newState = isOpen ? 'closed' : 'open';\n trigger.setAttribute('data-state', newState);\n trigger.setAttribute('aria-expanded', String(!isOpen));\n\n if (content) {\n if (isOpen) closeContent(content);\n else openContent(content);\n }\n });\n });\n });\n }\n\n initAccordions();\n document.addEventListener('astro:page-load', initAccordions);\n</script>\n"
"content": "---\nimport { cn } from '@/utils/cn';\nimport type { HTMLAttributes } from 'astro/types';\n\nexport interface Props extends HTMLAttributes<'div'> {\n type?: 'single' | 'multiple';\n}\n\nconst {\n type = 'single',\n class: className,\n ...props\n} = Astro.props;\n\nconst classes = cn('w-full', className);\n---\n\n<div class={classes} data-accordion data-type={type} {...props}>\n <slot />\n</div>\n\n<script>\n import { generateId } from '@/utils/focus-trap';\n\n const running = new WeakMap<HTMLElement, Animation>();\n\n function prefersReducedMotion() {\n return window.matchMedia('(prefers-reduced-motion: reduce)').matches;\n }\n\n function openContent(content: HTMLElement) {\n // If an animation is in flight, continue from its current height\n const prev = running.get(content);\n const startHeight = prev ? content.getBoundingClientRect().height : 0;\n prev?.cancel();\n content.hidden = false;\n content.setAttribute('data-state', 'open');\n if (prefersReducedMotion()) return;\n const anim = content.animate(\n [{ height: `${startHeight}px` }, { height: `${content.scrollHeight}px` }],\n { duration: 200, easing: 'ease-out' },\n );\n running.set(content, anim);\n }\n\n function closeContent(content: HTMLElement) {\n const prev = running.get(content);\n const startHeight = prev\n ? content.getBoundingClientRect().height\n : content.scrollHeight;\n prev?.cancel();\n content.setAttribute('data-state', 'closed');\n if (prefersReducedMotion()) {\n content.hidden = true;\n return;\n }\n const anim = content.animate(\n [{ height: `${startHeight}px` }, { height: '0px' }],\n { duration: 200, easing: 'ease-out' },\n );\n anim.onfinish = () => {\n content.hidden = true;\n };\n running.set(content, anim);\n }\n\n function initAccordions() {\n document.querySelectorAll('[data-accordion]').forEach((accordion) => {\n if (accordion.hasAttribute('data-initialized')) return;\n accordion.setAttribute('data-initialized', 'true');\n\n const type = accordion.getAttribute('data-type');\n \n // Only select direct accordion items (not nested ones)\n const items = Array.from(accordion.querySelectorAll('[data-accordion-item]')).filter(\n (item) => item.closest('[data-accordion]') === accordion\n );\n\n items.forEach((item) => {\n const trigger = item.querySelector(':scope > [data-accordion-trigger]') as HTMLElement;\n const content = item.querySelector(':scope > [data-accordion-content]') as HTMLElement;\n\n if (!trigger || !content) return;\n\n // Generate IDs and set aria-controls\n if (!content.id) {\n content.id = generateId('accordion-content');\n }\n if (!trigger.id) {\n trigger.id = generateId('accordion-trigger');\n }\n\n trigger.setAttribute('aria-controls', content.id);\n content.setAttribute('aria-labelledby', trigger.id);\n\n const labels = Array.from(trigger.querySelectorAll('[data-accordion-label]'));\n labels.forEach((label) => {\n if (!label.id) label.id = generateId('accordion-label');\n });\n const descriptions = Array.from(\n trigger.querySelectorAll('[data-accordion-description]')\n );\n descriptions.forEach((description) => {\n if (!description.id) description.id = generateId('accordion-description');\n });\n\n if (\n !trigger.hasAttribute('aria-label') &&\n !trigger.hasAttribute('aria-labelledby') &&\n labels.length > 0\n ) {\n trigger.setAttribute('aria-labelledby', labels.map((label) => label.id).join(' '));\n }\n\n if (!trigger.hasAttribute('aria-describedby') && descriptions.length > 0) {\n trigger.setAttribute(\n 'aria-describedby',\n descriptions.map((description) => description.id).join(' '),\n );\n }\n });\n\n // Only select triggers that belong directly to this accordion\n const triggers = Array.from(accordion.querySelectorAll('[data-accordion-trigger]')).filter(\n (trigger) => trigger.closest('[data-accordion]') === accordion\n ) as HTMLElement[];\n\n triggers.forEach((trigger) => {\n trigger.addEventListener('click', (e) => {\n // Prevent event from bubbling to parent accordions\n e.stopPropagation();\n \n const item = trigger.closest('[data-accordion-item]');\n const content = item?.querySelector(':scope > [data-accordion-content]') as HTMLElement;\n const isOpen = trigger.getAttribute('data-state') === 'open';\n\n if (type === 'single') {\n // Close all other items in THIS accordion only\n triggers.forEach((t) => {\n if (t !== trigger) {\n t.setAttribute('data-state', 'closed');\n t.setAttribute('aria-expanded', 'false');\n const otherItem = t.closest('[data-accordion-item]');\n const otherContent = otherItem?.querySelector(':scope > [data-accordion-content]') as HTMLElement;\n if (otherContent && !otherContent.hidden) {\n closeContent(otherContent);\n }\n }\n });\n }\n\n // Toggle current item\n const newState = isOpen ? 'closed' : 'open';\n trigger.setAttribute('data-state', newState);\n trigger.setAttribute('aria-expanded', String(!isOpen));\n\n if (content) {\n if (isOpen) closeContent(content);\n else openContent(content);\n }\n });\n });\n });\n }\n\n initAccordions();\n document.addEventListener('astro:page-load', initAccordions);\n</script>\n"
},
{
"name": "AccordionContent.astro",
"path": "accordion/AccordionContent.astro",
"content": "---\nimport { cn } from \"@/utils/cn\";\nimport type { HTMLAttributes } from \"astro/types\";\n\nexport interface Props extends HTMLAttributes<\"div\"> {\n defaultOpen?: boolean;\n id?: string;\n}\n\nconst { defaultOpen = false, class: className, id, ...props } = Astro.props;\n\nconst classes = cn(\n \"overflow-hidden text-sm transition-all\",\n className\n);\n---\n\n<div\n class={classes}\n data-accordion-content\n data-state={defaultOpen ? \"open\" : \"closed\"}\n role=\"region\"\n id={id}\n hidden={!defaultOpen}\n {...props}\n>\n <div class=\"pb-4 pt-0\">\n <slot />\n </div>\n</div>\n"
},
{
"name": "AccordionDescription.astro",
"path": "accordion/AccordionDescription.astro",
"content": "---\nimport { cn } from '@/utils/cn';\nimport type { HTMLAttributes } from 'astro/types';\n\nexport interface Props extends HTMLAttributes<'span'> {\n id?: string;\n}\n\nconst { class: className, id, ...props } = Astro.props;\n---\n\n<span\n class={cn('block text-sm font-normal text-muted-foreground', className)}\n data-accordion-description\n id={id}\n {...props}\n>\n <slot />\n</span>\n"
},
{
"name": "AccordionItem.astro",
"path": "accordion/AccordionItem.astro",
"content": "---\nimport { cn } from '@/utils/cn';\nimport type { HTMLAttributes } from 'astro/types';\n\nexport interface Props extends HTMLAttributes<'div'> {\n value: string;\n}\n\nconst {\n value,\n class: className,\n ...props\n} = Astro.props;\n\nconst classes = cn('border-b', className);\n---\n\n<div class={classes} data-accordion-item data-value={value} {...props}>\n <slot />\n</div>\n"
},
{
"name": "AccordionLabel.astro",
"path": "accordion/AccordionLabel.astro",
"content": "---\nimport { cn } from '@/utils/cn';\nimport type { HTMLAttributes } from 'astro/types';\n\nexport interface Props extends HTMLAttributes<'span'> {\n id?: string;\n}\n\nconst { class: className, id, ...props } = Astro.props;\n---\n\n<span class={cn('block', className)} data-accordion-label id={id} {...props}>\n <slot />\n</span>\n"
},
{
"name": "AccordionTrigger.astro",
"path": "accordion/AccordionTrigger.astro",
Expand Down
2 changes: 1 addition & 1 deletion public/registry/barrel.json

Large diffs are not rendered by default.

Loading