mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 10:21:05 +00:00
Merge pull request #43 from metonym/accordion
refactor(accordion): remove expando, inline functions
This commit is contained in:
commit
5ed4b24613
3 changed files with 36 additions and 40 deletions
|
@ -3,7 +3,7 @@
|
||||||
export { className as class };
|
export { className as class };
|
||||||
export let open = true;
|
export let open = true;
|
||||||
export let count = 4;
|
export let count = 4;
|
||||||
export let props = {};
|
export let style = undefined;
|
||||||
|
|
||||||
import ChevronRight16 from 'carbon-icons-svelte/lib/ChevronRight16';
|
import ChevronRight16 from 'carbon-icons-svelte/lib/ChevronRight16';
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
@ -13,7 +13,7 @@
|
||||||
const skeletonItems = Array.from({ length: open ? count - 1 : count });
|
const skeletonItems = Array.from({ length: open ? count - 1 : count });
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ul class={_class} {...props}>
|
<ul on:click on:mouseover on:mouseenter on:mouseleave {style} class={_class}>
|
||||||
{#if open}
|
{#if open}
|
||||||
<li class={cx('--accordion__item', '--accordion__item--active')}>
|
<li class={cx('--accordion__item', '--accordion__item--active')}>
|
||||||
<span class={cx('--accordion__heading')}>
|
<span class={cx('--accordion__heading')}>
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
<script>
|
<script>
|
||||||
let className = undefined;
|
let className = undefined;
|
||||||
export { className as class };
|
export { className as class };
|
||||||
export let props = {};
|
export let style = undefined;
|
||||||
|
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ul {...props} class={cx('--accordion', className)}>
|
<ul on:click on:mouseover on:mouseenter on:mouseleave {style} class={cx('--accordion', className)}>
|
||||||
<slot />
|
<slot />
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
@ -6,56 +6,52 @@
|
||||||
export let title = undefined;
|
export let title = undefined;
|
||||||
export let iconDescription = 'Expand/Collapse';
|
export let iconDescription = 'Expand/Collapse';
|
||||||
export let open = false;
|
export let open = false;
|
||||||
export let props = {};
|
export let style = undefined;
|
||||||
|
|
||||||
import { createEventDispatcher } from 'svelte';
|
|
||||||
import ChevronRight16 from 'carbon-icons-svelte/lib/ChevronRight16';
|
import ChevronRight16 from 'carbon-icons-svelte/lib/ChevronRight16';
|
||||||
import { cx } from '../../lib';
|
import { cx } from '../../lib';
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
|
||||||
let animation = undefined;
|
let animation = undefined;
|
||||||
|
|
||||||
function handleAnimationEnd(event) {
|
$: animation = open ? 'expanding' : 'collapsing';
|
||||||
animation = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleClick(event) {
|
|
||||||
animation = open ? 'collapsing' : 'expanding';
|
|
||||||
open = !open;
|
|
||||||
dispatch('headingclick', { open, event });
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleKeyDown(event) {
|
|
||||||
if (open && event.key === 'Escape') {
|
|
||||||
open = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$: _class = cx(
|
$: _class = cx(
|
||||||
'--accordion__item',
|
'--accordion__item',
|
||||||
open && '--accordion__item--active',
|
open && '--accordion__item--active',
|
||||||
animation && `--accordion__item--${animation}`,
|
animation && `--accordion__item--${animation}`,
|
||||||
className
|
className
|
||||||
);
|
);
|
||||||
$: accordionItemProps = {
|
|
||||||
type: 'button',
|
|
||||||
class: cx('--accordion__heading'),
|
|
||||||
title: iconDescription,
|
|
||||||
'aria-expanded': open,
|
|
||||||
onClick: handleClick,
|
|
||||||
onKeyDown: handleKeyDown
|
|
||||||
};
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<li class={_class} {...props} on:animationend on:animationend={handleAnimationEnd}>
|
<li
|
||||||
<slot name="expando" props={accordionItemProps}>
|
{style}
|
||||||
<button {...accordionItemProps} on:click={handleClick} on:keydown={handleKeyDown}>
|
class={_class}
|
||||||
<ChevronRight16 class={cx('--accordion__arrow')} aria-label={iconDescription} />
|
on:animationend
|
||||||
<div class={cx('--accordion__title')}>
|
on:animationend={() => {
|
||||||
<slot name="title">{title}</slot>
|
animation = undefined;
|
||||||
</div>
|
}}>
|
||||||
</button>
|
<button
|
||||||
</slot>
|
type="button"
|
||||||
|
class={cx('--accordion__heading')}
|
||||||
|
title={iconDescription}
|
||||||
|
aria-expanded={open}
|
||||||
|
on:click
|
||||||
|
on:click={() => {
|
||||||
|
open = !open;
|
||||||
|
}}
|
||||||
|
on:mouseover
|
||||||
|
on:mouseenter
|
||||||
|
on:mouseleave
|
||||||
|
on:keydown
|
||||||
|
on:keydown={event => {
|
||||||
|
if (open && event.key === 'Escape') {
|
||||||
|
open = false;
|
||||||
|
}
|
||||||
|
}}>
|
||||||
|
<ChevronRight16 class={cx('--accordion__arrow')} aria-label={iconDescription} />
|
||||||
|
<div class={cx('--accordion__title')}>
|
||||||
|
<slot name="title">{title}</slot>
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
<div class={cx('--accordion__content')}>
|
<div class={cx('--accordion__content')}>
|
||||||
<slot />
|
<slot />
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue