~starkingdoms/starkingdoms

ref: 9e32164288a31f45041247ae4744c6461472e226 starkingdoms/starkingdoms-client/src/components/ui/Popup.svelte -rw-r--r-- 3.4 KiB
9e321642 — ghostlyzsh stronger module break off 1 year, 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<script lang="ts">
  import { onMount } from "svelte";
  import MovableIcon from "../../icons/MovableIcon.svelte";
  import ChevronDown from "../../icons/ChevronDown.svelte";
  import ChevronUp from "../../icons/ChevronUp.svelte";
  import Button from "./Button.svelte";

  let clazz = "";
  export { clazz as class };

  export let style = "";
  export let centered = false;
  export let id;
  export let title;
  export let notitle = false;
  export let draggable = false;
  export let minimizable = false;

  let header;
  let popup;

  let pos1 = 0;
  let pos2 = 0;
  let pos3 = 0;
  let pos4 = 0;

  let minimized = false;

  onMount(() => {
    if (draggable) {
      let top = window.localStorage.getItem(`pop-${id}top`);
      let left = window.localStorage.getItem(`pop-${id}left`);

      popup.style.top = top;
      popup.style.left = left;
    }
    if (minimizable) {
      minimized = window.localStorage.getItem(`pop-${id}minim`) === "yes";
    }
  });

  function dragMouseDown(e) {
    if (!draggable) {
      return;
    }
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e.preventDefault();
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    popup.style.top = popup.offsetTop - pos2 + "px";
    popup.style.left = popup.offsetLeft - pos1 + "px";

    window.localStorage.setItem(`pop-${id}top`, popup.style.top);
    window.localStorage.setItem(`pop-${id}left`, popup.style.left);
  }

  function closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;

    window.localStorage.setItem(`pop-${id}top`, popup.style.top);
    window.localStorage.setItem(`pop-${id}left`, popup.style.left);
  }

  function toggleMinimized() {
    if (minimizable) {
      minimized = !minimized;
      window.localStorage.setItem(`pop-${id}minim`, minimized ? "yes" : "no");
    }
  }
</script>

<div bind:this={popup} class="popup {clazz}" class:centered {style} {id}>
  {#if !notitle}
    <div bind:this={header} on:mousedown|preventDefault={dragMouseDown}>
      <h1>
        {#if draggable}
          <MovableIcon class="hdr-icon" />
        {/if}
        {title}
      </h1>
      {#if minimizable}
        <Button class="hdrbtn" on:click={toggleMinimized}>
          {#if minimized}
            <ChevronDown class="hdrbtn-icon" />
          {:else}
            <ChevronUp class="hdrbtn-icon" />
          {/if}
        </Button>
      {/if}
    </div>
  {/if}
  {#if !minimized}
    <slot />
  {/if}
</div>

<style lang="scss">
  .popup {
    background-color: var(--bg-secondary-1);

    padding: 1em;

    height: min-content;

    border-radius: 5px;
    z-index: 100000;
  }

  :global(.hdr-icon) {
    color: var(--headline);
    vertical-align: middle;
    display: inline-block;
    width: 1rem;
    height: 1rem;
    margin-bottom: 0.1rem;
  }

  :global(.hdrbtn) {
    position: absolute;
    width: 2rem;
    height: 2rem;
    right: 1em;
    top: 1em;
  }

  :global(.hdrbtn-icon) {
    color: var(--links);
    width: 1.5rem;
    height: 1.5rem;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
  }

  .popup :global(h1) {
    margin: 0;
  }
  .popup :global(h2) {
    margin: 0 0 0.5em;
    color: var(--sub-headline);
  }

  .centered {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
  }
</style>