Dynamic tabs

Switch between components with <Dynamic>; each tab mounts and unmounts.

App.kn
<script lang="ts">
  import { Dynamic } from '@inflector/keeno'
  import Profile from './Profile.kn'
  import Settings from './Settings.kn'
  import Stats from './Stats.kn'

  const tabs = [
    { id: 'profile', label: 'Profile', component: Profile },
    { id: 'stats', label: 'Stats', component: Stats },
    { id: 'settings', label: 'Settings', component: Settings },
  ]
  const active = state('profile')
  const current = derived(() => tabs.find((t) => t.id === active)!)
</script>

<nav role="tablist">
  {tabs.map((t) => (
    <button key={t.id} role="tab" aria-selected={active === t.id} class={{ on: active === t.id }} onClick={() => active.set(t.id)}>
      {t.label}
    </button>
  ))}
</nav>

<!-- <Dynamic> renders whichever component is current and passes it the other props. -->
<div class="panel" role="tabpanel">
  <Dynamic component={current.component} user="Ada" />
</div>

<style>
  nav {
    display: flex;
    gap: 2px;
    padding: 3px;
    border: 1px solid var(--border);
    border-radius: 0.625rem;
    background: var(--surface);
    width: fit-content;
  }
  nav button {
    min-height: 1.75rem;
    border: 1px solid transparent;
    border-radius: 0.4rem;
    background: none;
    box-shadow: none;
    color: var(--muted);
  }
  nav button:hover:not(:disabled) {
    border-color: transparent;
    background: none;
    color: var(--fg);
  }
  nav button.on,
  nav button.on:hover:not(:disabled) {
    border-color: var(--border);
    background: var(--bg);
    color: var(--fg);
    box-shadow: 0 1px 2px rgb(0 0 0 / 0.08);
  }
  .panel {
    margin-top: 1rem;
    padding: 1rem;
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    min-height: 120px;
  }
</style>