impr: improve select menu semantics

This commit is contained in:
James Xylight committed 2025-08-29 16:09:22 -07:00
1 parent f105240559
commit ebf53faa10
13 files changed
+170 -187

No files matched your search

+45 -48
View File
@@ -88,53 +88,50 @@
</Option>
</Select>
{#if selected?.startsWith('Top')}
<div transition:fly={{ easing: backOut, x: 2 }}>
<Select
class="border-l-0 rounded-l-none"
bind:value={selected}
onchange={() => {
sort = 'TopAll'
if (navigate)
searchParam(page.url, 'sort', selected, 'page', 'cursor')
}}
>
{#snippet customLabel()}
<span class="flex items-center gap-1">
<Icon src={Clock} size="15" micro />
{$t('filter.sort.top.time.label')}
</span>
{/snippet}
<Option value="TopAll" icon={PlusCircle}>
{$t('filter.sort.top.time.all')}
</Option>
<Option value="TopNineMonths" icon={Calendar}>
{$t('filter.sort.top.time.9months')}
</Option>
<Option value="TopSixMonths" icon={Calendar}>
{$t('filter.sort.top.time.6months')}
</Option>
<Option value="TopThreeMonths" icon={Calendar}>
{$t('filter.sort.top.time.3months')}
</Option>
<Option value="TopMonth" icon={CalendarDays}>
{$t('filter.sort.top.time.month')}
</Option>
<Option value="TopWeek" icon={CalendarDays}>
{$t('filter.sort.top.time.week')}
</Option>
<Option value="TopDay" icon={Sun}>
{$t('filter.sort.top.time.day')}
</Option>
<Option value="TopTwelveHour" icon={Clock}>
{$t('filter.sort.top.time.12hours')}
</Option>
<Option value="TopSixHour" icon={Clock}>
{$t('filter.sort.top.time.6hours')}
</Option>
<Option value="TopHour" icon={Clock}>
{$t('filter.sort.top.time.hour')}
</Option>
</Select>
</div>
<Select
class="border-l-0 rounded-l-none"
bind:value={selected}
onchange={() => {
sort = 'TopAll'
if (navigate) searchParam(page.url, 'sort', selected, 'page', 'cursor')
}}
>
{#snippet customLabel()}
<span class="flex items-center gap-1">
<Icon src={Clock} size="15" micro />
{$t('filter.sort.top.time.label')}
</span>
{/snippet}
<Option value="TopAll" icon={PlusCircle}>
{$t('filter.sort.top.time.all')}
</Option>
<Option value="TopNineMonths" icon={Calendar}>
{$t('filter.sort.top.time.9months')}
</Option>
<Option value="TopSixMonths" icon={Calendar}>
{$t('filter.sort.top.time.6months')}
</Option>
<Option value="TopThreeMonths" icon={Calendar}>
{$t('filter.sort.top.time.3months')}
</Option>
<Option value="TopMonth" icon={CalendarDays}>
{$t('filter.sort.top.time.month')}
</Option>
<Option value="TopWeek" icon={CalendarDays}>
{$t('filter.sort.top.time.week')}
</Option>
<Option value="TopDay" icon={Sun}>
{$t('filter.sort.top.time.day')}
</Option>
<Option value="TopTwelveHour" icon={Clock}>
{$t('filter.sort.top.time.12hours')}
</Option>
<Option value="TopSixHour" icon={Clock}>
{$t('filter.sort.top.time.6hours')}
</Option>
<Option value="TopHour" icon={Clock}>
{$t('filter.sort.top.time.hour')}
</Option>
</Select>
{/if}
</div>
@@ -144,8 +144,9 @@
>
{#if showCommunity && community}
<Popover>
{#snippet target()}
{#snippet target(attachment)}
<button
{@attach attachment}
class={[
'row-span-2 shrink-0 mr-2 self-center group/btn',
'bg-slate-200 dark:bg-zinc-800 rounded-lg cursor-pointer',
@@ -177,6 +178,7 @@
rounding="2xl"
elevation="high"
class="max-w-2xl w-full max-h-128 overflow-auto"
data-autoclose="false"
>
{#await import('../community/CommunityHeader.svelte') then { default: CommunityHeader }}
<CommunityHeader {community} {subscribed} />
+1 -1
View File
@@ -41,7 +41,7 @@
'transition-transform duration-300 ease-out',
]}
>
<Icon src={open ? Minus : Plus} size="16" micro class={[]} />
<Icon src={open ? Minus : Plus} size="15" micro class={[]} />
</div>
{/if}
<div
+3 -9
View File
@@ -10,23 +10,21 @@
<script lang="ts">
import { page } from '$app/state'
import { t } from '$lib/i18n/translations'
import { Button, Material, toast } from 'mono-svelte'
import { focusTrap } from 'svelte-focus-trap'
import { Icon, Share, XMark } from 'svelte-hero-icons'
import { expoOut } from 'svelte/easing'
import { fade, scale } from 'svelte/transition'
import { focusTrap } from 'svelte-focus-trap'
import { t } from '$lib/i18n/translations'
interface Props {
/**
* The full-resolution image URL
*/
alt?: string
class?: string
children?: import('svelte').Snippet
}
let { alt = '', class: clazz = '', children }: Props = $props()
let { alt = '' }: Props = $props()
</script>
{#if page.state.openImage || '' != ''}
@@ -88,7 +86,3 @@
</div>
</div>
{/if}
<button onclick={() => history.back()} class="contents {clazz}">
{@render children?.()}
</button>
+1 -1
View File
@@ -142,7 +142,7 @@
<Spinner width={20} />
</div>
{:then { default: Profile }}
<Profile />
<Profile />
{/await}
{/if}
{/snippet}
+25 -28
View File
@@ -133,34 +133,31 @@
{/snippet}
{$t('nav.menu.settings')}
</MenuButton>
<!--svelte-ignore a11y_click_events_have_key_events-->
<!--svelte-ignore a11y_no_static_element_interactions-->
<div onclick={e => e.stopPropagation()}>
<Select bind:value={theme.colorScheme} size="sm">
{#snippet target(attachment)}
<MenuButton
{@attach attachment}
icon={theme.colorScheme == 'system'
? ComputerDesktop
: theme.colorScheme == 'light'
? Sun
: Moon}
class=" w-full"
>
{$t('nav.menu.colorscheme.label')}
</MenuButton>
<Option value="system" class="hidden" icon={ComputerDesktop}>
{$t('nav.menu.colorscheme.system')}
</Option>
<Option value="light" class="hidden" icon={Sun}>
{$t('nav.menu.colorscheme.light')}
</Option>
<Option value="dark" class="hidden" icon={Moon}>
{$t('nav.menu.colorscheme.dark')}
</Option>
{/snippet}
</Select>
</div>
<Select bind:value={theme.colorScheme} size="sm" placement="left">
{#snippet target(attachment)}
<MenuButton
{@attach attachment}
icon={theme.colorScheme == 'system'
? ComputerDesktop
: theme.colorScheme == 'light'
? Sun
: Moon}
class=" w-full"
nest
>
{$t('nav.menu.colorscheme.label')}
</MenuButton>
<Option value="system" class="hidden" icon={ComputerDesktop}>
{$t('nav.menu.colorscheme.system')}
</Option>
<Option value="light" class="hidden" icon={Sun}>
{$t('nav.menu.colorscheme.light')}
</Option>
<Option value="dark" class="hidden" icon={Moon}>
{$t('nav.menu.colorscheme.dark')}
</Option>
{/snippet}
</Select>
<MenuButton href="/theme">
{#snippet prefix()}
<Icon src={Swatch} size="16" micro />
+4 -1
View File
@@ -11,8 +11,10 @@
import {
ArrowLeftOnRectangle,
Bookmark,
ChevronUpDown,
Cog6Tooth,
ComputerDesktop,
Icon,
Identification,
Inbox,
Moon,
@@ -92,7 +94,7 @@
: theme.colorScheme == 'light'
? Sun
: Moon}
class=" w-full"
class="w-full relative"
>
<Option value="system" class="hidden" icon={ComputerDesktop}>
{$t('nav.menu.colorscheme.system')}
@@ -103,6 +105,7 @@
<Option value="dark" class="hidden" icon={Moon}>
{$t('nav.menu.colorscheme.dark')}
</Option>
<Icon micro size="16" src={ChevronUpDown} class="ml-auto" />
</SidebarButton>
{/snippet}
</Select>
+5 -7
View File
@@ -1,5 +1,5 @@
<script lang="ts">
import type { HTMLAttributes } from 'svelte/elements'
import type { ClassValue, HTMLAttributes } from 'svelte/elements'
interface Props extends HTMLAttributes<HTMLSpanElement> {
for?: string | undefined
@@ -7,7 +7,7 @@
* The `text` prop will take precedence over the slot.
*/
text?: string | undefined
class?: string
class?: ClassValue
children?: import('svelte').Snippet
}
@@ -20,16 +20,14 @@
}: Props = $props()
</script>
<svelte:element
this={forID ? 'label' : 'span'}
<label
{...rest}
for={forID}
class="text-sm text-slate-800 dark:text-zinc-200 font-medium w-full {clazz ||
''}"
class={['text-sm text-slate-800 dark:text-zinc-200 font-medium', clazz]}
>
{#if text}
{text}
{:else if children}
{@render children?.()}
{/if}
</svelte:element>
</label>
+46 -52
View File
@@ -35,12 +35,14 @@
<script lang="ts" generics="T">
import { setContext, tick, type Snippet } from 'svelte'
import type { Placement } from 'svelte-floating-ui/core'
import {
CheckCircle,
ChevronUpDown,
Icon,
type IconSource,
} from 'svelte-hero-icons'
import type { Attachment } from 'svelte/attachments'
import type { ClassValue, HTMLSelectAttributes } from 'svelte/elements'
import {
buttonColor,
@@ -52,9 +54,6 @@
import Menu from '../../popover/Menu.svelte'
import MenuButton from '../../popover/MenuButton.svelte'
import Label from '../Label.svelte'
import { generateID } from '../helper.js'
import type { Placement } from 'svelte-floating-ui/core'
import type { Attachment } from 'svelte/attachments'
let open = $state(false)
let element: HTMLSelectElement | undefined = $state()
@@ -79,7 +78,6 @@
label = undefined,
size = 'md',
shadow = 'none',
id = generateID(),
class: clazz = '',
baseClass = '',
selectClass = '',
@@ -89,53 +87,57 @@
oncontextmenu,
onchange,
placement = 'bottom',
target,
target: passedTarget,
...rest
}: Props<T> = $props()
</script>
{#snippet selectTarget(attachment: Attachment)}
<select
{@attach attachment}
{...rest}
{id}
bind:this={element}
class={[
buttonSize[size],
buttonShadow[shadow],
buttonColor.secondary,
'appearance-none transition-colors rounded-xl text-sm w-full min-w-full cursor-pointer pr-6',
selectClass,
clazz,
]}
bind:value
onmousedown={e => {
e.preventDefault()
}}
onkeypress={e => {
e.preventDefault()
open = !open
}}
{onchange}
{oncontextmenu}
{placeholder}
>
{#if placeholder}
<option disabled selected value="">{placeholder}</option>
{/if}
{@render children?.()}
</select>
<Label text={label} class={['space-y-1 relative', clazz, baseClass]}>
{@render customLabel?.()}
<Icon
src={ChevronUpDown}
micro
size="16"
class="absolute right-1.5 bottom-1 box-border pointer-events-none z-10 text-slate-600 dark:text-zinc-400"
/>
<select
{@attach attachment}
{...rest}
bind:this={element}
class={[
buttonSize[size],
buttonShadow[shadow],
buttonColor.secondary,
'appearance-none transition-colors rounded-xl text-sm w-full min-w-full cursor-pointer pr-6',
selectClass,
clazz,
]}
bind:value
onmousedown={e => {
e.preventDefault()
}}
onkeypress={e => {
e.preventDefault()
open = !open
}}
{onchange}
{oncontextmenu}
{placeholder}
>
{#if placeholder}
<option disabled selected value="">{placeholder}</option>
{/if}
{@render children?.()}
</select>
</Label>
{/snippet}
<div class={['flex flex-col gap-1', clazz, baseClass]}>
{#if customLabel || label}
<Label for={id} text={label}>
{@render customLabel?.()}
</Label>
{/if}
<div class="w-full relative">
<Menu bind:open {placement} target={target ?? selectTarget}>
<Menu bind:open {placement}>
{#snippet target(attachment)}
{@const render = passedTarget ?? selectTarget}
{@render render?.(attachment)}
{/snippet}
{#each context.options as option (option)}
{#if customOption}{@render customOption({
option,
@@ -179,11 +181,3 @@
{/if}
{/each}
</Menu>
<Icon
src={ChevronUpDown}
micro
size="16"
class="absolute right-0 top-1/4 mx-1.5 box-border pointer-events-none"
/>
</div>
</div>
-5
View File
@@ -14,7 +14,6 @@
interface Props {
openOnHover?: boolean
open?: boolean
manual?: boolean
placement?: Placement
middleware?: Middleware[]
strategy?: Strategy
@@ -26,11 +25,9 @@
let {
openOnHover = false,
open = $bindable(false),
manual = false,
placement = 'bottom-start',
middleware = [offset(6), shift({ padding: 6 }), flip()],
strategy = 'absolute',
class: clazz = '',
target,
children,
...rest
@@ -42,12 +39,10 @@
{placement}
{middleware}
{strategy}
{manual}
{target}
{...rest}
bind:open
popoverClass="rounded-xl w-full backdrop-blur-md max-w-72"
class={clazz}
>
{#snippet popover()}
<div
+7 -1
View File
@@ -5,7 +5,7 @@
type ButtonColor,
type ButtonProps,
} from '../button/Button.svelte'
import { Icon, type IconSource } from 'svelte-hero-icons'
import { ChevronRight, Icon, type IconSource } from 'svelte-hero-icons'
import type { ClassValue } from 'svelte/elements'
interface Props extends ButtonProps {
@@ -18,6 +18,7 @@
prefix?: Snippet
children?: Snippet
suffix?: Snippet
nest?: boolean
}
let {
@@ -30,6 +31,7 @@
prefix: passedPrefix,
children,
suffix: passedSuffix,
nest,
...rest
}: Props = $props()
</script>
@@ -48,6 +50,7 @@
{alignment}
{href}
{disabled}
data-autoclose={nest ? 'false' : 'true'}
shadow="none"
>
{#snippet prefix()}
@@ -67,5 +70,8 @@
{@render children?.()}
{#snippet suffix()}
{@render passedSuffix?.()}
{#if nest}
<Icon src={ChevronRight} size="16" micro class="ml-auto" />
{/if}
{/snippet}
</Button>
+5 -8
View File
@@ -82,16 +82,13 @@
const clickHandler = (event: Event) => {
if (!e) return
if (!e?.contains(event.target as Node | null)) {
if (!autoClose) {
// @ts-expect-error svelte hell
if (popoverEl && !popoverEl.contains(event.target)) {
open = false
}
} else {
const target = event.target as Element | null
if (!e?.contains(event.target as Element | null)) {
if (target?.closest("[data-autoclose='false']") == null) {
open = false
document.removeEventListener('click', clickHandler)
}
document.removeEventListener('click', clickHandler)
}
}
document.addEventListener('click', clickHandler)