2020-05-07 15:30:04 +02:00
|
|
|
<script>
|
2020-06-27 17:56:01 +02:00
|
|
|
import { onMount, beforeUpdate, afterUpdate } from "svelte"
|
|
|
|
import Portal from "svelte-portal"
|
2020-06-02 22:31:29 +02:00
|
|
|
import { buildStyle } from "../../helpers.js"
|
2020-05-28 17:24:53 +02:00
|
|
|
export let options = []
|
2020-05-07 15:30:04 +02:00
|
|
|
export let value = ""
|
2020-05-28 17:24:53 +02:00
|
|
|
export let styleBindingProperty
|
2020-05-07 15:30:04 +02:00
|
|
|
export let onChange = value => {}
|
2020-05-28 17:24:53 +02:00
|
|
|
|
|
|
|
let open = null
|
|
|
|
let rotate = ""
|
|
|
|
let select
|
|
|
|
let selectMenu
|
|
|
|
let icon
|
|
|
|
|
2020-06-27 17:56:01 +02:00
|
|
|
let selectAnchor = null;
|
|
|
|
let dimensions = {top: 0, bottom: 0, left: 0}
|
2020-05-28 17:24:53 +02:00
|
|
|
|
|
|
|
let positionSide = "top"
|
2020-06-27 17:56:01 +02:00
|
|
|
let maxHeight = 0
|
|
|
|
let scrollTop = 0;
|
|
|
|
let containerEl = null;
|
2020-05-08 10:57:41 +02:00
|
|
|
|
2020-05-19 18:00:53 +02:00
|
|
|
const handleStyleBind = value =>
|
|
|
|
!!styleBindingProperty ? { style: `${styleBindingProperty}: ${value}` } : {}
|
|
|
|
|
2020-05-07 15:30:04 +02:00
|
|
|
onMount(() => {
|
2020-05-28 17:24:53 +02:00
|
|
|
if (select) {
|
2020-06-29 14:38:08 +02:00
|
|
|
select.addEventListener("keydown", handleEnter)
|
|
|
|
selectMenu.addEventListener("keydown", handleEscape)
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
2020-06-27 17:56:01 +02:00
|
|
|
|
|
|
|
|
2020-05-28 17:24:53 +02:00
|
|
|
return () => {
|
2020-06-29 14:38:08 +02:00
|
|
|
select.removeEventListener("keydown", handleEnter)
|
|
|
|
selectMenu.removeEventListener("keydown", handleEscape)
|
2020-05-07 15:30:04 +02:00
|
|
|
}
|
2020-06-27 17:56:01 +02:00
|
|
|
|
2020-05-07 15:30:04 +02:00
|
|
|
})
|
2020-05-28 17:24:53 +02:00
|
|
|
|
2020-06-29 14:38:08 +02:00
|
|
|
function handleEscape(e) {
|
|
|
|
if(open && e.key === "Escape") {
|
|
|
|
toggleSelect(false)
|
|
|
|
}
|
2020-06-27 17:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getDimensions() {
|
|
|
|
const { bottom, top: spaceAbove, left } = selectAnchor.getBoundingClientRect()
|
2020-05-28 17:24:53 +02:00
|
|
|
const spaceBelow = window.innerHeight - bottom
|
|
|
|
|
2020-06-27 17:56:01 +02:00
|
|
|
let y;
|
|
|
|
|
2020-05-28 17:24:53 +02:00
|
|
|
if (spaceAbove > spaceBelow) {
|
|
|
|
positionSide = "bottom"
|
2020-06-27 17:56:01 +02:00
|
|
|
maxHeight = spaceAbove - 20
|
|
|
|
y = (window.innerHeight - spaceAbove)
|
2020-05-28 17:24:53 +02:00
|
|
|
} else {
|
|
|
|
positionSide = "top"
|
2020-06-27 17:56:01 +02:00
|
|
|
y = bottom
|
|
|
|
maxHeight = spaceBelow - 20
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
2020-06-27 17:56:01 +02:00
|
|
|
|
|
|
|
dimensions = {[positionSide]: y, left}
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 14:38:08 +02:00
|
|
|
function handleEnter(e) {
|
|
|
|
if (!open && e.key === "Enter") {
|
2020-05-28 17:24:53 +02:00
|
|
|
toggleSelect(true)
|
2020-06-29 14:38:08 +02:00
|
|
|
}
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function toggleSelect(isOpen) {
|
2020-06-27 17:56:01 +02:00
|
|
|
getDimensions()
|
2020-05-28 17:24:53 +02:00
|
|
|
if (isOpen) {
|
|
|
|
icon.style.transform = "rotate(180deg)"
|
2020-06-29 14:38:08 +02:00
|
|
|
selectMenu.focus()
|
2020-05-28 17:24:53 +02:00
|
|
|
} else {
|
|
|
|
icon.style.transform = "rotate(0deg)"
|
|
|
|
}
|
2020-06-27 17:56:01 +02:00
|
|
|
open = isOpen
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 17:56:01 +02:00
|
|
|
|
2020-05-28 17:24:53 +02:00
|
|
|
function handleClick(val) {
|
|
|
|
value = val
|
|
|
|
onChange(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
$: menuStyle = buildStyle({
|
2020-06-27 17:56:01 +02:00
|
|
|
"max-height": `${maxHeight.toFixed(0)}px`,
|
2020-05-29 11:45:19 +02:00
|
|
|
"transform-origin": `center ${positionSide}`,
|
2020-06-27 17:56:01 +02:00
|
|
|
[positionSide]: `${dimensions[positionSide]}px`,
|
|
|
|
"left": `${dimensions.left.toFixed(0)}px`,
|
2020-05-28 17:24:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
$: isOptionsObject = options.every(o => typeof o === "object")
|
|
|
|
|
|
|
|
$: selectedOption = isOptionsObject
|
|
|
|
? options.find(o => o.value === value)
|
|
|
|
: {}
|
|
|
|
|
|
|
|
$: displayLabel =
|
|
|
|
selectedOption && selectedOption.label ? selectedOption.label : value || ""
|
2020-05-07 15:30:04 +02:00
|
|
|
</script>
|
|
|
|
|
2020-05-28 17:24:53 +02:00
|
|
|
<div
|
|
|
|
tabindex="0"
|
|
|
|
bind:this={select}
|
|
|
|
class="bb-select-container"
|
|
|
|
on:click={() => toggleSelect(!open)}>
|
2020-06-27 17:56:01 +02:00
|
|
|
<div bind:this={selectAnchor} class="bb-select-anchor selected">
|
2020-05-28 17:24:53 +02:00
|
|
|
<span>{displayLabel}</span>
|
|
|
|
<i bind:this={icon} class="ri-arrow-down-s-fill" />
|
|
|
|
</div>
|
2020-06-27 17:56:01 +02:00
|
|
|
<Portal>
|
|
|
|
<div
|
2020-06-29 14:38:08 +02:00
|
|
|
tabindex="0"
|
2020-06-27 17:56:01 +02:00
|
|
|
bind:this={selectMenu}
|
|
|
|
style={menuStyle}
|
|
|
|
class="bb-select-menu"
|
|
|
|
class:open>
|
|
|
|
<ul>
|
|
|
|
{#if isOptionsObject}
|
|
|
|
{#each options as { value: v, label }}
|
|
|
|
<li
|
|
|
|
{...handleStyleBind(v)}
|
|
|
|
on:click|self={handleClick(v)}
|
|
|
|
class:selected={value === v}>
|
|
|
|
{label}
|
|
|
|
</li>
|
|
|
|
{/each}
|
|
|
|
{:else}
|
|
|
|
{#each options as v}
|
|
|
|
<li
|
|
|
|
{...handleStyleBind(v)}
|
|
|
|
on:click|self={handleClick(v)}
|
|
|
|
class:selected={value === v}>
|
|
|
|
{v}
|
|
|
|
</li>
|
|
|
|
{/each}
|
|
|
|
{/if}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</Portal>
|
2020-05-28 17:24:53 +02:00
|
|
|
</div>
|
|
|
|
{#if open}
|
|
|
|
<div on:click|self={() => toggleSelect(false)} class="overlay" />
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.overlay {
|
2020-06-27 17:56:01 +02:00
|
|
|
position: fixed;
|
2020-05-28 17:24:53 +02:00
|
|
|
top: 0;
|
|
|
|
bottom: 0;
|
|
|
|
right: 0;
|
|
|
|
left: 0;
|
|
|
|
z-index: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
.bb-select-container {
|
|
|
|
outline: none;
|
2020-05-30 19:48:20 +02:00
|
|
|
width: 160px;
|
2020-06-23 22:29:18 +02:00
|
|
|
height: 36px;
|
2020-05-28 17:24:53 +02:00
|
|
|
cursor: pointer;
|
2020-06-23 22:29:18 +02:00
|
|
|
font-size: 14px;
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.bb-select-anchor {
|
|
|
|
cursor: pointer;
|
|
|
|
display: flex;
|
2020-06-23 22:29:18 +02:00
|
|
|
padding: 0px 12px;
|
|
|
|
height: 36px;
|
2020-06-23 09:19:16 +02:00
|
|
|
background-color: var(--grey-2);
|
|
|
|
border-radius: 5px;
|
2020-06-01 17:31:58 +02:00
|
|
|
align-items: center;
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.bb-select-anchor > span {
|
2020-06-23 09:19:16 +02:00
|
|
|
color: var(--ink);
|
|
|
|
font-weight: 400;
|
2020-05-30 19:48:20 +02:00
|
|
|
width: 140px;
|
2020-05-29 11:45:19 +02:00
|
|
|
overflow-x: hidden;
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.bb-select-anchor > i {
|
2020-05-29 11:45:19 +02:00
|
|
|
transition: transform 0.13s ease;
|
|
|
|
transform-origin: center;
|
2020-05-30 19:48:20 +02:00
|
|
|
width: 20px;
|
|
|
|
height: 20px;
|
2020-05-29 11:45:19 +02:00
|
|
|
text-align: center;
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.selected {
|
2020-06-23 09:19:16 +02:00
|
|
|
color: var(--ink);
|
|
|
|
font-weight: 400;
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
.bb-select-menu {
|
|
|
|
position: absolute;
|
|
|
|
display: flex;
|
2020-06-29 14:38:08 +02:00
|
|
|
outline: none;
|
2020-05-28 17:24:53 +02:00
|
|
|
box-sizing: border-box;
|
|
|
|
flex-direction: column;
|
|
|
|
opacity: 0;
|
2020-05-30 19:48:20 +02:00
|
|
|
width: 160px;
|
2020-05-28 17:24:53 +02:00
|
|
|
z-index: 2;
|
2020-06-23 09:19:16 +02:00
|
|
|
color: var(--ink);
|
|
|
|
font-weight: 400;
|
2020-05-28 17:24:53 +02:00
|
|
|
height: fit-content !important;
|
|
|
|
border-bottom-left-radius: 2px;
|
|
|
|
border-bottom-right-radius: 2px;
|
2020-06-23 09:19:16 +02:00
|
|
|
background-color: var(--grey-2);
|
2020-05-28 17:24:53 +02:00
|
|
|
transform: scale(0);
|
|
|
|
transition: opacity 0.13s linear, transform 0.12s cubic-bezier(0, 0, 0.2, 1);
|
|
|
|
overflow-y: auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
.open {
|
|
|
|
transform: scale(1);
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ul {
|
|
|
|
list-style-type: none;
|
|
|
|
margin: 0;
|
2020-05-29 11:49:51 +02:00
|
|
|
padding: 5px 0px;
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
li {
|
|
|
|
height: auto;
|
|
|
|
padding: 5px 0px;
|
|
|
|
cursor: pointer;
|
2020-06-02 22:31:29 +02:00
|
|
|
padding-left: 10px;
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 11:45:19 +02:00
|
|
|
li:hover {
|
2020-06-02 22:31:29 +02:00
|
|
|
background-color: #e6e6e6;
|
2020-05-28 17:24:53 +02:00
|
|
|
}
|
|
|
|
</style>
|