LoginSignup
0
2

More than 3 years have passed since last update.

SvelteでCollapseを実装する

Posted at

Reactではreact-collapse、Vueではvue2-transitionsを使用してCollapseの動きをつけてたが、Svelteならsvelte/transitionのslideを使用することで楽勝で実装できた。

できたものはこんな感じ。
svelte.gif

コード量はこんなもん(装飾(style)は除く)

<script>
import { slide } from 'svelte/transition'

const contents = ['content1', 'content2', 'content3']
let showContent = ''

const handleClick = (payload) => {
  // 現状開いてる場合は閉じる
  showContent = payload === showContent ? '' : payload
}
</script>

{#each contents as content}
<div>
  <button type="button" on:click={() => handleClick(content)}>{content}</button>
  {#if showContent === content}
  <p transition:slide={{ duration: 400 }}>{content}</p>
  {/if}
</div>
{/each}

一応、作ったものへのリンクを貼っておく
https://svelte.dev/repl/90d1f85a07134b3aa7d9d265f836d771?version=3.24.0

SvelteはMotion/Transitionがかなり充実してるので、カルーセルとかも案外簡単に自作できそう。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2