Merge pull request #43 from metonym/accordion

refactor(accordion): remove expando, inline functions
This commit is contained in:
Eric Liu 2019-12-19 19:44:08 -08:00 committed by GitHub
commit 5ed4b24613
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 40 deletions

View file

@ -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')}>

View file

@ -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>

View file

@ -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}
on:animationend
on:animationend={() => {
animation = undefined;
}}>
<button
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} /> <ChevronRight16 class={cx('--accordion__arrow')} aria-label={iconDescription} />
<div class={cx('--accordion__title')}> <div class={cx('--accordion__title')}>
<slot name="title">{title}</slot> <slot name="title">{title}</slot>
</div> </div>
</button> </button>
</slot>
<div class={cx('--accordion__content')}> <div class={cx('--accordion__content')}>
<slot /> <slot />
</div> </div>