impr: merge mono-ui with photon (#455)
This commit is contained in:
1 parent
16075f72f2
commit
180d6fcc0d
40 files changed
+2024
-1
No files matched your search
@@ -0,0 +1,52 @@
|
||||
<script lang="ts">
|
||||
import Material from '../materials/Material.svelte'
|
||||
import Popover, { type Origin } from '../popover/Popover.svelte'
|
||||
import {
|
||||
offset,
|
||||
type Middleware,
|
||||
type Placement,
|
||||
shift,
|
||||
type Strategy,
|
||||
flip,
|
||||
autoPlacement,
|
||||
size,
|
||||
} from '@floating-ui/core'
|
||||
|
||||
export let openOnHover: boolean = false
|
||||
export let open: boolean = false
|
||||
|
||||
export let placement: Placement = 'bottom-start'
|
||||
export let middleware: Middleware[] = [
|
||||
offset(6),
|
||||
shift({ padding: 6 }),
|
||||
flip(),
|
||||
]
|
||||
export let strategy: Strategy = 'absolute'
|
||||
</script>
|
||||
|
||||
<Popover
|
||||
{openOnHover}
|
||||
{placement}
|
||||
{middleware}
|
||||
{strategy}
|
||||
{...$$restProps}
|
||||
bind:open
|
||||
popoverClass="backdrop-blur-xl rounded-xl w-full max-w-[280px]"
|
||||
class={$$props.class}
|
||||
>
|
||||
<slot name="target" slot="target" />
|
||||
<div
|
||||
slot="popover"
|
||||
class="w-full max-w-[280px] origin-top-left
|
||||
max-h-[32rem] overflow-auto list-none shadow-lg rounded-xl"
|
||||
>
|
||||
<Material
|
||||
class="flex flex-col py-2 list-none bg-white/80 dark:bg-zinc-900/80 px-2 gap-px"
|
||||
color="distinct"
|
||||
padding="none"
|
||||
rounding="xl"
|
||||
>
|
||||
<slot />
|
||||
</Material>
|
||||
</div>
|
||||
</Popover>
|
||||
@@ -0,0 +1,38 @@
|
||||
<script lang="ts">
|
||||
import Button, {
|
||||
type ButtonAlignment,
|
||||
type ButtonColor,
|
||||
} from '../button/Button.svelte'
|
||||
|
||||
export let color: ButtonColor = 'tertiary'
|
||||
export let alignment: ButtonAlignment = 'left'
|
||||
export let href: string | undefined = undefined
|
||||
export let disabled: boolean = false
|
||||
</script>
|
||||
|
||||
<Button
|
||||
{...$$restProps}
|
||||
on:click
|
||||
{color}
|
||||
rounding="none"
|
||||
class="w-full px-2 rounded-lg min-h-[36px] font-normal {disabled
|
||||
? 'opacity-70 pointer-events-none cursor-not-allowed'
|
||||
: ''} {color == 'tertiary'
|
||||
? 'hover:dark:bg-zinc-800/70'
|
||||
: ''} duration-100 {$$props.class}"
|
||||
{alignment}
|
||||
{href}
|
||||
{disabled}
|
||||
>
|
||||
<span
|
||||
class="contents {color == 'tertiary'
|
||||
? 'text-slate-600 dark:text-zinc-400'
|
||||
: ''}
|
||||
flex-shrink-0"
|
||||
slot="prefix"
|
||||
>
|
||||
<slot name="prefix" />
|
||||
</span>
|
||||
<slot />
|
||||
<slot name="suffix" slot="suffix" />
|
||||
</Button>
|
||||
@@ -0,0 +1,6 @@
|
||||
<div
|
||||
class="text-slate-800 dark:text-zinc-200 text-xs font-medium py-2 px-2 flex items-center gap-1"
|
||||
>
|
||||
<div><slot /></div>
|
||||
<hr class="flex-shrink flex-1 border-slate-200 dark:border-zinc-800 border-opacity-70" />
|
||||
</div>
|
||||
@@ -0,0 +1,120 @@
|
||||
<script context="module" lang="ts">
|
||||
export type Origin = keyof typeof popoverOrigins
|
||||
|
||||
export const popoverOrigins = {
|
||||
'top-right': 'bottom-full right-0 origin-bottom-right',
|
||||
'bottom-left': 'top-full origin-top-left',
|
||||
'top-left': 'bottom-full left-0 origin-bottom-left',
|
||||
'bottom-right': 'top-full right-0 origin-top-right',
|
||||
'top-center': 'bottom-full -left-[450%] origin-bottom',
|
||||
'bottom-center': 'top-full -left-[450%] origin-top',
|
||||
}
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import { Material } from '../index'
|
||||
import { backOut, elasticIn, elasticOut, expoOut } from 'svelte/easing'
|
||||
import { fly, scale } from 'svelte/transition'
|
||||
import { createFloatingActions } from 'svelte-floating-ui'
|
||||
import {
|
||||
offset,
|
||||
type Middleware,
|
||||
flip,
|
||||
shift,
|
||||
type Placement,
|
||||
type Strategy,
|
||||
size,
|
||||
} from '@floating-ui/core'
|
||||
import Portal from './Portal.svelte'
|
||||
import { focusTrap } from 'svelte-focus-trap'
|
||||
import { tick } from 'svelte'
|
||||
|
||||
export let openOnHover: boolean = false
|
||||
export let open = false
|
||||
export let placement: Placement = 'bottom-start'
|
||||
export let middleware: Middleware[] = [offset(6), shift(), flip()]
|
||||
export let strategy: Strategy = 'fixed'
|
||||
export let manual: boolean = false
|
||||
|
||||
let canUseContents = true
|
||||
|
||||
let el: any
|
||||
|
||||
const [floatingRef, floatingContent] = createFloatingActions({
|
||||
strategy: strategy,
|
||||
placement: placement,
|
||||
middleware: middleware,
|
||||
})
|
||||
|
||||
const customFloatingContent = (node: HTMLDivElement) => {
|
||||
// if (!canUseContents) return
|
||||
|
||||
floatingContent(node)
|
||||
}
|
||||
|
||||
const customFloatingRef = (node: HTMLButtonElement) => {
|
||||
const n = node.children.item(0)
|
||||
|
||||
// @ts-ignore
|
||||
if (n) floatingRef(n)
|
||||
else {
|
||||
canUseContents = false
|
||||
floatingRef(node)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:body
|
||||
on:click={(e) => {
|
||||
if (openOnHover) return
|
||||
|
||||
if (!el?.contains(e.target)) {
|
||||
open = false
|
||||
}
|
||||
}}
|
||||
on:keydown={async (e) => {
|
||||
if (open && e.key == 'Escape') {
|
||||
open = false
|
||||
el?.firstChild.focus()
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
{#if $$slots.target}
|
||||
<button
|
||||
on:mouseover={() => (openOnHover ? (open = true) : false)}
|
||||
on:mouseleave={() => (openOnHover ? (open = false) : false)}
|
||||
on:focus={() => (openOnHover ? (open = true) : false)}
|
||||
on:focusout={() => (openOnHover ? (open = false) : false)}
|
||||
on:click={() => (!openOnHover && !manual ? (open = !open) : false)}
|
||||
on:keydown={(e) => {
|
||||
if (e.key == 'Escape') open = false
|
||||
}}
|
||||
role="menu"
|
||||
type="button"
|
||||
class="{canUseContents
|
||||
? 'contents text-left'
|
||||
: 'w-max h-max'} {$$props.class}"
|
||||
bind:this={el}
|
||||
use:customFloatingRef
|
||||
>
|
||||
<slot name="target" />
|
||||
</button>
|
||||
{/if}
|
||||
|
||||
{#if open}
|
||||
<Portal class="z-[150]">
|
||||
<div
|
||||
transition:scale={{ duration: 200, start: 0.96, easing: backOut }}
|
||||
class="z-[150] {$$props.popoverClass}"
|
||||
use:customFloatingContent
|
||||
use:focusTrap
|
||||
>
|
||||
<slot name="popover">
|
||||
<Material elevation="high" color="distinct" class="flex flex-col">
|
||||
<slot />
|
||||
</Material>
|
||||
</slot>
|
||||
</div>
|
||||
</Portal>
|
||||
{/if}
|
||||
@@ -0,0 +1,34 @@
|
||||
<script lang="ts">
|
||||
import { browser } from '$app/environment'
|
||||
|
||||
// https://github.com/sveltejs/svelte/issues/3088#issuecomment-505785516
|
||||
import { onMount, onDestroy, createEventDispatcher } from 'svelte'
|
||||
let slottedElements: HTMLDivElement, portal: HTMLDivElement
|
||||
|
||||
const dispatch = createEventDispatcher<{ mounted: void }>()
|
||||
|
||||
onMount(() => {
|
||||
if (!browser) return
|
||||
portal = document?.createElement('div')
|
||||
if (portal) portal.className = 'portal-mount'
|
||||
document?.body.appendChild(portal)
|
||||
portal?.appendChild(slottedElements)
|
||||
dispatch('mounted')
|
||||
})
|
||||
|
||||
onDestroy(() => {
|
||||
portal.remove()
|
||||
})
|
||||
</script>
|
||||
|
||||
<div class="portal-initial-mount-point">
|
||||
<div bind:this={slottedElements} class="portal-content {$$props.class || ''}">
|
||||
<slot />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.portal-initial-mount-point {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user