cleanup: use context api for select menus and options
This commit is contained in:
1 parent
ee08787afc
commit
dd9abe5345
20 files changed
+271
-336
No files matched your search
@@ -13,9 +13,10 @@
|
||||
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
||||
import ImagePreviewInput from '$lib/components/input/ImagePreviewInput.svelte'
|
||||
import Label from 'mono-svelte/forms/Label.svelte'
|
||||
import { DocumentPlus, Icon } from 'svelte-hero-icons'
|
||||
import { DocumentPlus, GlobeAlt, Icon, MapPin } from 'svelte-hero-icons'
|
||||
import ImageUploadModal from '../modal/ImageUploadModal.svelte'
|
||||
import Select from 'mono-svelte/forms/select/Select.svelte'
|
||||
import Option from 'mono-svelte/forms/select/Option.svelte'
|
||||
|
||||
interface Props {
|
||||
/**
|
||||
@@ -221,8 +222,8 @@
|
||||
Only moderators can post
|
||||
</Switch>
|
||||
<Select label="Visibility" class="w-max" bind:value={formData.visibility}>
|
||||
<option value="Public">Public</option>
|
||||
<option value="LocalOnly">Local Only</option>
|
||||
<Option icon={GlobeAlt} value="Public">Public</Option>
|
||||
<Option icon={MapPin} value="LocalOnly">Local Only</Option>
|
||||
</Select>
|
||||
<Button
|
||||
submit
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
} from 'svelte-hero-icons'
|
||||
import { amModOfAny } from '../moderation/moderation'
|
||||
import { t } from '$lib/translations'
|
||||
import Option from 'mono-svelte/forms/select/Option.svelte'
|
||||
|
||||
interface Props {
|
||||
selected: string
|
||||
@@ -45,42 +46,22 @@
|
||||
</span>
|
||||
{/if}
|
||||
{/snippet}
|
||||
<option value="All">
|
||||
<Icon
|
||||
src={GlobeAmericas}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
<Option value="All" icon={GlobeAmericas}>
|
||||
{$t('filter.location.all')}
|
||||
</option>
|
||||
<option value="Local">
|
||||
<Icon
|
||||
src={MapPin}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>{$t('filter.location.local')}
|
||||
</option>
|
||||
<option value="Subscribed" disabled={$profile?.jwt == undefined}>
|
||||
<Icon
|
||||
src={Newspaper}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="Local" icon={MapPin}>{$t('filter.location.local')}</Option>
|
||||
<Option
|
||||
value="Subscribed"
|
||||
disabled={$profile?.jwt == undefined}
|
||||
icon={Newspaper}
|
||||
>
|
||||
{$t('filter.location.subscribed')}
|
||||
</option>
|
||||
<option
|
||||
</Option>
|
||||
<Option
|
||||
value="ModeratorView"
|
||||
disabled={!$profile?.jwt || !amModOfAny($profile?.user)}
|
||||
icon={ShieldCheck}
|
||||
>
|
||||
<Icon
|
||||
src={ShieldCheck}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
{$t('filter.location.moderator')}
|
||||
</option>
|
||||
</Option>
|
||||
</Select>
|
||||
@@ -1,11 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { run } from 'svelte/legacy'
|
||||
|
||||
import { page } from '$app/stores'
|
||||
import { settings } from '$lib/settings.svelte'
|
||||
import { t } from '$lib/translations'
|
||||
import { searchParam } from '$lib/util.svelte.js'
|
||||
import type { SortType } from 'lemmy-js-client'
|
||||
import { Select } from 'mono-svelte'
|
||||
import {
|
||||
ArrowTrendingDown,
|
||||
@@ -26,6 +22,7 @@
|
||||
} from 'svelte-hero-icons'
|
||||
import { backOut } from 'svelte/easing'
|
||||
import { fly, slide } from 'svelte/transition'
|
||||
import Option from 'mono-svelte/forms/select/Option.svelte'
|
||||
|
||||
interface Props {
|
||||
selected: string
|
||||
@@ -63,87 +60,33 @@
|
||||
{$t('filter.sort.label')}
|
||||
</span>
|
||||
{/snippet}
|
||||
<option value="Active">
|
||||
<Icon
|
||||
src={ArrowTrendingUp}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
<Option value="Active" icon={ArrowTrendingUp}>
|
||||
{$t('filter.sort.active')}
|
||||
</option>
|
||||
<option value="Hot">
|
||||
<Icon
|
||||
src={Fire}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="Hot" icon={Fire}>
|
||||
{$t('filter.sort.hot')}
|
||||
</option>
|
||||
<option value="Scaled">
|
||||
<Icon
|
||||
src={Scale}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="Scaled" icon={Scale}>
|
||||
{$t('filter.sort.scaled')}
|
||||
</option>
|
||||
<option value="TopAll">
|
||||
<Icon
|
||||
src={Trophy}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="TopAll" icon={Trophy}>
|
||||
{$t('filter.sort.top.label')}
|
||||
</option>
|
||||
<option value="New">
|
||||
<Icon
|
||||
src={Star}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="New" icon={Star}>
|
||||
{$t('filter.sort.new')}
|
||||
</option>
|
||||
<option value="Old">
|
||||
<Icon
|
||||
src={Clock}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="Old" icon={Clock}>
|
||||
{$t('filter.sort.old')}
|
||||
</option>
|
||||
<option value="Controversial">
|
||||
<Icon
|
||||
src={ArrowTrendingDown}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="Controversial" icon={ArrowTrendingDown}>
|
||||
{$t('filter.sort.controversial')}
|
||||
</option>
|
||||
<option value="MostComments">
|
||||
<Icon
|
||||
src={ChatBubbleOvalLeft}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="MostComments" icon={ChatBubbleOvalLeft}>
|
||||
{$t('filter.sort.mostcomments')}
|
||||
</option>
|
||||
<option value="NewComments">
|
||||
<Icon
|
||||
src={ChatBubbleLeftRight}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="NewComments" icon={ChatBubbleLeftRight}>
|
||||
{$t('filter.sort.newcomments')}
|
||||
</option>
|
||||
</Option>
|
||||
</Select>
|
||||
{#if selected?.startsWith('Top')}
|
||||
<div transition:fly={{ easing: backOut, x: 2 }}>
|
||||
@@ -162,89 +105,36 @@
|
||||
{$t('filter.sort.top.time.label')}
|
||||
</span>
|
||||
{/snippet}
|
||||
<option value="TopAll">
|
||||
<Icon
|
||||
src={PlusCircle}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>{$t('filter.sort.top.time.all')}
|
||||
</option>
|
||||
<option value="TopNineMonths">
|
||||
<Icon
|
||||
src={Calendar}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
<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
|
||||
src={Calendar}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="TopSixMonths" icon={Calendar}>
|
||||
{$t('filter.sort.top.time.6months')}
|
||||
</option>
|
||||
<option value="TopThreeMonths">
|
||||
<Icon
|
||||
src={Calendar}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="TopThreeMonths" icon={Calendar}>
|
||||
{$t('filter.sort.top.time.3months')}
|
||||
</option>
|
||||
<option value="TopMonth">
|
||||
<Icon
|
||||
src={CalendarDays}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>{$t('filter.sort.top.time.month')}
|
||||
</option>
|
||||
<option value="TopWeek">
|
||||
<Icon
|
||||
src={CalendarDays}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>{$t('filter.sort.top.time.week')}
|
||||
</option>
|
||||
<option value="TopDay">
|
||||
<Icon
|
||||
src={Sun}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>{$t('filter.sort.top.time.day')}
|
||||
</option>
|
||||
<option value="TopTwelveHour">
|
||||
<Icon
|
||||
src={Clock}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>{$t('filter.sort.top.time.12hours')}
|
||||
</option>
|
||||
<option value="TopSixHour">
|
||||
<Icon
|
||||
src={Clock}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>{$t('filter.sort.top.time.6hours')}
|
||||
</option>
|
||||
<option value="TopHour">
|
||||
<Icon
|
||||
src={Clock}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>{$t('filter.sort.top.time.hour')}
|
||||
</option>
|
||||
</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>
|
||||
{/if}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { settings } from '$lib/settings.svelte.js'
|
||||
import { t } from '$lib/translations'
|
||||
import { Select } from 'mono-svelte'
|
||||
import Option from 'mono-svelte/forms/select/Option.svelte'
|
||||
import {
|
||||
Bars3,
|
||||
Clock,
|
||||
@@ -30,31 +31,13 @@
|
||||
</span>
|
||||
{/if}
|
||||
{/snippet}
|
||||
<option value="cozy">
|
||||
<Icon
|
||||
src={RectangleGroup}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
<Option value="cozy" icon={RectangleGroup}>
|
||||
{$t('filter.view.cozy')}
|
||||
</option>
|
||||
<option value="list">
|
||||
<Icon
|
||||
src={QueueList}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="list" icon={QueueList}>
|
||||
{$t('filter.view.list')}
|
||||
</option>
|
||||
<option value="compact">
|
||||
<Icon
|
||||
src={Bars3}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Option>
|
||||
<Option value="compact" icon={Bars3}>
|
||||
{$t('filter.view.compact')}
|
||||
</option>
|
||||
</Option>
|
||||
</Select>
|
||||
@@ -2,6 +2,7 @@
|
||||
import { profile } from '$lib/auth.svelte.js'
|
||||
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
|
||||
import Markdown from '$lib/components/markdown/Markdown.svelte'
|
||||
import TextProps from '$lib/components/ui/text/TextProps.svelte'
|
||||
import type { PrivateMessageView } from 'lemmy-js-client'
|
||||
|
||||
interface Props {
|
||||
@@ -34,5 +35,7 @@
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
<Markdown source={message.private_message.content} />
|
||||
<TextProps wrap="no-wrap">
|
||||
<Markdown source={message.private_message.content} />
|
||||
</TextProps>
|
||||
</div>
|
||||
@@ -17,6 +17,7 @@
|
||||
import { amMod, isAdmin } from './moderation'
|
||||
import { Button, Checkbox, Modal } from 'mono-svelte'
|
||||
import { t } from '$lib/translations'
|
||||
import Option from 'mono-svelte/forms/select/Option.svelte'
|
||||
|
||||
interface Props {
|
||||
open: boolean
|
||||
@@ -230,9 +231,9 @@
|
||||
{$t('comment.reply')}
|
||||
<Select bind:value={preset} placeholder="No preset">
|
||||
{#each settings.moderation.presets as preset}
|
||||
<option value={preset.content}>
|
||||
<Option value={preset.content}>
|
||||
{preset.title}
|
||||
</option>
|
||||
</Option>
|
||||
{/each}
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
} from '$lib/components/error/ErrorContainer.svelte'
|
||||
import { errorMessage } from '$lib/lemmy/error'
|
||||
import FreeTextInput from '$lib/components/input/FreeTextInput.svelte'
|
||||
import Option from 'mono-svelte/forms/select/Option.svelte'
|
||||
|
||||
interface Props {
|
||||
edit?: boolean
|
||||
@@ -437,12 +438,12 @@
|
||||
label={$t('settings.app.lang.title')}
|
||||
bind:value={data.language_id}
|
||||
>
|
||||
<option value={undefined}>
|
||||
<Option value={undefined}>
|
||||
<Icon src={XMark} size="16" micro />
|
||||
{$t('form.post.unset')}
|
||||
</option>
|
||||
</Option>
|
||||
{#each $site?.all_languages as language}
|
||||
<option value={language.id.toString()}>{language.name}</option>
|
||||
<Option value={language.id.toString()}>{language.name}</Option>
|
||||
{/each}
|
||||
</Select>
|
||||
{:else}
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
import SiteCard from '$lib/components/lemmy/SiteCard.svelte'
|
||||
import { t } from '$lib/translations'
|
||||
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
|
||||
import Option from 'mono-svelte/forms/select/Option.svelte'
|
||||
|
||||
let showInstance = $state(false)
|
||||
|
||||
@@ -164,18 +165,15 @@
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
||||
<div class="contents" onclick={stopPropagation(() => {})}>
|
||||
<Select bind:value={$colorScheme} class="ml-auto my-auto w-24" size="sm">
|
||||
<option value="system">
|
||||
<Icon src={ComputerDesktop} size="16" micro />
|
||||
<Option value="system" icon={ComputerDesktop}>
|
||||
{$t('nav.menu.colorscheme.system')}
|
||||
</option>
|
||||
<option value="light">
|
||||
<Icon src={Sun} size="16" micro />
|
||||
</Option>
|
||||
<Option value="light" icon={Sun}>
|
||||
{$t('nav.menu.colorscheme.light')}
|
||||
</option>
|
||||
<option value="dark">
|
||||
<Icon src={Moon} size="16" micro />
|
||||
</Option>
|
||||
<Option value="dark" icon={Moon}>
|
||||
{$t('nav.menu.colorscheme.dark')}
|
||||
</option>
|
||||
</Option>
|
||||
</Select>
|
||||
</div>
|
||||
</MenuButton>
|
||||
|
||||
@@ -226,10 +226,10 @@
|
||||
afterNavigate(() => {
|
||||
// open = false
|
||||
})
|
||||
run(() => {
|
||||
$effect(() => {
|
||||
if (open) search = ''
|
||||
})
|
||||
run(() => {
|
||||
$effect(() => {
|
||||
groups = getGroups(
|
||||
$resumables,
|
||||
$profile,
|
||||
@@ -241,7 +241,7 @@
|
||||
let flattenedActions = $derived(
|
||||
filteredGroups.flatMap((group) => group.actions),
|
||||
)
|
||||
run(() => {
|
||||
$effect(() => {
|
||||
debouncedSearch(search)
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -68,6 +68,7 @@
|
||||
import type { HTMLButtonAttributes } from 'svelte/elements'
|
||||
|
||||
import Spinner from '../loader/Spinner.svelte'
|
||||
import type { Snippet } from 'svelte'
|
||||
|
||||
type ButtonRoundness = 'pill' | 'xl' | 'lg' | 'md' | 'none'
|
||||
type ButtonRoundingSide = 'all' | 'left' | 'right' | 'top' | 'bottom'
|
||||
@@ -114,7 +115,7 @@
|
||||
},
|
||||
}
|
||||
|
||||
interface Props {
|
||||
interface Props extends Omit<HTMLButtonAttributes, 'prefix'> {
|
||||
loading?: boolean
|
||||
submit?: boolean
|
||||
color?: ButtonColor
|
||||
@@ -128,9 +129,9 @@
|
||||
loaderWidth?: number | undefined
|
||||
href?: string | undefined
|
||||
class?: string
|
||||
prefix?: import('svelte').Snippet
|
||||
children?: import('svelte').Snippet
|
||||
suffix?: import('svelte').Snippet
|
||||
prefix?: Snippet
|
||||
children?: Snippet
|
||||
suffix?: Snippet
|
||||
onclick?: HTMLButtonAttributes['onclick']
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<script lang="ts" generics="T">
|
||||
import { getContext, onMount, type Snippet } from 'svelte'
|
||||
import type { HTMLAttributes, HTMLOptionAttributes } from 'svelte/elements'
|
||||
import type { IconSource } from 'svelte-hero-icons'
|
||||
|
||||
interface Props extends Omit<HTMLOptionAttributes, 'prefix'> {
|
||||
children: Snippet
|
||||
icon?: IconSource
|
||||
}
|
||||
let { children, icon, ...rest }: Props = $props()
|
||||
|
||||
let optionElement = $state<HTMLOptionElement>()
|
||||
let option: any = $derived({
|
||||
value: optionElement?.value ?? '',
|
||||
label: optionElement?.innerText?.trim() ?? '',
|
||||
icon: icon,
|
||||
disabled: false,
|
||||
})
|
||||
|
||||
interface SelectContext {
|
||||
options: {
|
||||
value: string
|
||||
label: string
|
||||
icon?: IconSource
|
||||
disabled: boolean
|
||||
}[]
|
||||
}
|
||||
|
||||
const context = getContext<SelectContext>('select')
|
||||
|
||||
$effect(() => {
|
||||
if (!context.options.find((v) => v == option)) {
|
||||
context.options.push(option)
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<option {...rest} bind:this={optionElement}>
|
||||
{@render children()}
|
||||
</option>
|
||||
@@ -20,25 +20,23 @@
|
||||
ChevronDown,
|
||||
ChevronUpDown,
|
||||
Icon,
|
||||
type IconSource,
|
||||
} from 'svelte-hero-icons'
|
||||
import { browser } from '$app/environment'
|
||||
import { writable } from 'svelte/store'
|
||||
|
||||
let open = $state(false)
|
||||
let element: HTMLSelectElement | undefined = $state()
|
||||
|
||||
let options: { value: any; label: string | null; disabled: boolean }[] =
|
||||
$state([])
|
||||
interface SelectContext {
|
||||
options: {
|
||||
value: string
|
||||
label: string
|
||||
icon?: IconSource
|
||||
disabled?: boolean
|
||||
}[]
|
||||
}
|
||||
|
||||
// Capture all options from the select element
|
||||
$effect(() => {
|
||||
options = element?.options
|
||||
? Array.from(element?.options).map((option) => ({
|
||||
value: option.value,
|
||||
label: option.innerHTML,
|
||||
disabled: option.disabled,
|
||||
}))
|
||||
: []
|
||||
const context = setContext<SelectContext>('select', {
|
||||
options: [],
|
||||
})
|
||||
|
||||
const dispatcher = createEventDispatcher<{
|
||||
@@ -46,8 +44,9 @@
|
||||
contextmenu: any
|
||||
input: any
|
||||
}>()
|
||||
|
||||
interface Props {
|
||||
value?: T | undefined
|
||||
value?: T | string | undefined
|
||||
placeholder?: string | undefined
|
||||
label?: string | undefined
|
||||
size?: ButtonSize
|
||||
@@ -112,7 +111,7 @@
|
||||
</select>
|
||||
{/snippet}
|
||||
|
||||
{#each options as option}
|
||||
{#each context.options as option}
|
||||
{#if customOption}{@render customOption({
|
||||
option,
|
||||
selected: option.value == value,
|
||||
@@ -129,6 +128,14 @@
|
||||
? '!bg-slate-100 dark:!bg-zinc-800'
|
||||
: ''}"
|
||||
>
|
||||
{#if option.icon}
|
||||
<Icon
|
||||
src={option.icon}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
{/if}
|
||||
{@html option.label}
|
||||
{#if option.value == value}
|
||||
<Icon
|
||||
|
||||
Reference in new issue
Block a user