|
1 | 1 | <script lang="ts">
|
2 |
| - import { spring } from 'svelte/motion'; |
| 2 | + import { Spring } from 'svelte/motion'; |
3 | 3 | import { SplitPane, type Length } from '@rich_harris/svelte-split-pane';
|
4 | 4 |
|
5 | 5 | const UNIT_REGEX = /(\d+)(?:(px|rem|%|em))/i;
|
6 | 6 |
|
7 |
| - export let panel: string; |
| 7 | + interface Props { |
| 8 | + panel: string; |
| 9 | + pos?: Length; |
| 10 | + min?: Length; |
| 11 | + max?: Length; |
| 12 | + main?: import('svelte').Snippet; |
| 13 | + header?: import('svelte').Snippet; |
| 14 | + body?: import('svelte').Snippet; |
| 15 | + } |
8 | 16 |
|
9 |
| - export let pos: Length = '90%'; |
| 17 | + let { |
| 18 | + panel, |
| 19 | + pos = $bindable('90%'), |
| 20 | + min = '4.2rem', |
| 21 | + max = '-4.2rem', |
| 22 | + main, |
| 23 | + header, |
| 24 | + body |
| 25 | + }: Props = $props(); |
10 | 26 |
|
11 |
| - $: previous_pos = Math.min(+pos.replace(UNIT_REGEX, '$1'), 70); |
| 27 | + let previous_pos = Math.min(normalize(pos), 70); |
12 | 28 |
|
13 |
| - export let max: Length = '90%'; |
| 29 | + let container: HTMLElement; |
14 | 30 |
|
15 | 31 | // we can't bind to the spring itself, but we
|
16 | 32 | // can still use the spring to drive `pos`
|
17 |
| - const driver = spring(+pos.replace(UNIT_REGEX, '$1'), { |
| 33 | + const driver = new Spring(normalize(pos), { |
18 | 34 | stiffness: 0.2,
|
19 | 35 | damping: 0.5
|
20 | 36 | });
|
21 | 37 |
|
22 |
| - // @ts-ignore |
23 |
| - $: pos = $driver + '%'; |
| 38 | + $effect(() => { |
| 39 | + pos = driver.current + '%'; |
| 40 | + }); |
24 | 41 |
|
25 | 42 | const toggle = () => {
|
26 |
| - const numeric_pos = +pos.replace(UNIT_REGEX, '$1'); |
| 43 | + const pc = normalize(pos); |
| 44 | + const px = pc * 0.01 * container.clientHeight; |
27 | 45 |
|
28 |
| - driver.set(numeric_pos, { hard: true }); |
| 46 | + const open = container.clientHeight - px > 42; |
29 | 47 |
|
30 |
| - if (numeric_pos > 80) { |
31 |
| - driver.set(previous_pos); |
32 |
| - } else { |
33 |
| - previous_pos = numeric_pos; |
| 48 | + driver.set(pc, { hard: true }); |
| 49 | +
|
| 50 | + if (open) { |
| 51 | + previous_pos = pc; |
34 | 52 | driver.set(100);
|
| 53 | + } else { |
| 54 | + driver.set(previous_pos); |
35 | 55 | }
|
36 | 56 | };
|
| 57 | +
|
| 58 | + function normalize(pos: string) { |
| 59 | + let normalized = +pos.replace(UNIT_REGEX, '$1'); |
| 60 | +
|
| 61 | + if (normalized < 0) { |
| 62 | + normalized += 100; |
| 63 | + } |
| 64 | +
|
| 65 | + return normalized; |
| 66 | + } |
37 | 67 | </script>
|
38 | 68 |
|
39 |
| -<SplitPane {max} min="10%" type="vertical" bind:pos> |
40 |
| - {#snippet a()} |
41 |
| - <section> |
42 |
| - <slot name="main" /> |
43 |
| - </section> |
44 |
| - {/snippet} |
45 |
| - |
46 |
| - {#snippet b()} |
47 |
| - <section> |
48 |
| - <div class="panel-header"> |
49 |
| - <button class="panel-heading" on:click={toggle}>{panel}</button> |
50 |
| - <slot name="panel-header" /> |
51 |
| - </div> |
52 |
| - |
53 |
| - <div class="panel-body"> |
54 |
| - <slot name="panel-body" /> |
55 |
| - </div> |
56 |
| - </section> |
57 |
| - {/snippet} |
58 |
| -</SplitPane> |
| 69 | +<div class="container" bind:this={container}> |
| 70 | + <SplitPane {min} {max} type="vertical" bind:pos> |
| 71 | + {#snippet a()} |
| 72 | + <section> |
| 73 | + {@render main?.()} |
| 74 | + </section> |
| 75 | + {/snippet} |
| 76 | + |
| 77 | + {#snippet b()} |
| 78 | + <section> |
| 79 | + <div class="panel-header"> |
| 80 | + <button class="panel-heading raised" onclick={toggle}> |
| 81 | + <svg |
| 82 | + width="1.8rem" |
| 83 | + height="1.8rem" |
| 84 | + viewBox="0 0 24 24" |
| 85 | + fill="none" |
| 86 | + stroke="currentColor" |
| 87 | + stroke-width="2" |
| 88 | + stroke-linecap="round" |
| 89 | + stroke-linejoin="round" |
| 90 | + > |
| 91 | + <path d="m7 15 5 5 5-5" /> |
| 92 | + <path d="m7 9 5-5 5 5" /> |
| 93 | + </svg> |
| 94 | + |
| 95 | + {panel} |
| 96 | + </button> |
| 97 | + |
| 98 | + {@render header?.()} |
| 99 | + </div> |
| 100 | + |
| 101 | + <div class="panel-body"> |
| 102 | + {@render body?.()} |
| 103 | + </div> |
| 104 | + </section> |
| 105 | + {/snippet} |
| 106 | + </SplitPane> |
| 107 | +</div> |
59 | 108 |
|
60 | 109 | <style>
|
| 110 | + .container { |
| 111 | + width: 100%; |
| 112 | + height: 100%; |
| 113 | + } |
| 114 | +
|
61 | 115 | .panel-header {
|
62 | 116 | height: var(--sk-pane-controls-height);
|
63 | 117 | display: flex;
|
|
74 | 128 | .panel-heading {
|
75 | 129 | font: var(--sk-font-ui-small);
|
76 | 130 | text-transform: uppercase;
|
77 |
| - flex: 1; |
| 131 | + height: 3.2rem; |
| 132 | + padding: 0 0.8rem; |
| 133 | + /* flex: 1; */ |
78 | 134 | text-align: left;
|
| 135 | + display: flex; |
| 136 | + align-items: center; |
| 137 | + gap: 0.4rem; |
79 | 138 | }
|
80 | 139 |
|
81 | 140 | section {
|
|
0 commit comments