Merge pull request #411 from Budibase/colorpicker/swatch-updates

Swatch and Title Attributes Updates / Fixes
This commit is contained in:
Conor_Mack 2020-07-02 16:33:00 +01:00 committed by GitHub
commit 8d8eb83ae6
4 changed files with 68 additions and 39 deletions

View File

@ -87,6 +87,7 @@
"babel-jest": "^24.8.0", "babel-jest": "^24.8.0",
"browser-sync": "^2.26.7", "browser-sync": "^2.26.7",
"cypress": "^4.8.0", "cypress": "^4.8.0",
"eslint-plugin-cypress": "^2.11.1",
"http-proxy-middleware": "^0.19.1", "http-proxy-middleware": "^0.19.1",
"jest": "^24.8.0", "jest": "^24.8.0",
"ncp": "^2.0.0", "ncp": "^2.0.0",
@ -109,4 +110,4 @@
"svelte-jester": "^1.0.6" "svelte-jester": "^1.0.6"
}, },
"gitHead": "115189f72a850bfb52b65ec61d932531bf327072" "gitHead": "115189f72a850bfb52b65ec61d932531bf327072"
} }

View File

@ -3,7 +3,7 @@
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
import Swatch from "./Swatch.svelte" import Swatch from "./Swatch.svelte"
import CheckedBackground from "./CheckedBackground.svelte" import CheckedBackground from "./CheckedBackground.svelte"
import {buildStyle} from "../helpers.js" import { buildStyle } from "../helpers.js"
import { import {
getColorFormat, getColorFormat,
convertToHSVA, convertToHSVA,
@ -14,11 +14,12 @@
import ButtonGroup from "./ButtonGroup.svelte" import ButtonGroup from "./ButtonGroup.svelte"
import Input from "./Input.svelte" import Input from "./Input.svelte"
import Portal from "./Portal.svelte" import Portal from "./Portal.svelte"
import {keyevents} from "../actions" import { keyevents } from "../actions"
export let value = "#3ec1d3ff" export let value = "#3ec1d3ff"
export let open = false export let open = false
export let swatches = [] //TODO: Safe swatches - limit to 12. warn in console export let swatches = []
export let disableSwatches = false export let disableSwatches = false
export let format = "hexa" export let format = "hexa"
export let style = "" export let style = ""
@ -27,6 +28,7 @@
let colorPicker = null let colorPicker = null
let adder = null let adder = null
let swatchesSetFromLocalStore = false
let h = 0 let h = 0
let s = 0 let s = 0
@ -38,7 +40,14 @@
onMount(() => { onMount(() => {
if (!swatches.length > 0) { if (!swatches.length > 0) {
//Don't use locally stored recent colors if swatches have been passed as props //Don't use locally stored recent colors if swatches have been passed as props
getRecentColors() swatchesSetFromLocalStore = true
swatches = getRecentColors() || []
}
if (swatches.length > 12) {
console.warn(
`Colorpicker - ${swatches.length} swatches were provided. Only the first 12 swatches will be displayed.`
)
} }
if (colorPicker) { if (colorPicker) {
@ -53,24 +62,21 @@
function getRecentColors() { function getRecentColors() {
let colorStore = localStorage.getItem("cp:recent-colors") let colorStore = localStorage.getItem("cp:recent-colors")
if (colorStore) { if (colorStore) {
swatches = JSON.parse(colorStore) return JSON.parse(colorStore)
} }
} }
function handleEscape() { function handleEscape() {
if(open) { if (open) {
open = false; open = false
} }
} }
function setRecentColor(color) { function setRecentColors(color) {
if (swatches.length === 12) { const s = swatchesSetFromLocalStore
swatches.splice(0, 1) ? swatches
} : [...getRecentColors(), color]
if (!swatches.includes(color)) { localStorage.setItem("cp:recent-colors", JSON.stringify(s))
swatches = [...swatches, color]
localStorage.setItem("cp:recent-colors", JSON.stringify(swatches))
}
} }
function convertAndSetHSVA() { function convertAndSetHSVA() {
@ -134,8 +140,16 @@
function addSwatch() { function addSwatch() {
if (format) { if (format) {
if (swatches.length === 12) {
swatches.splice(0, 1)
}
if (!swatches.includes(value)) {
swatches = [...swatches, value]
setRecentColors(value)
}
dispatch("addswatch", value) dispatch("addswatch", value)
setRecentColor(value)
} }
} }
@ -143,7 +157,10 @@
let removedSwatch = swatches.splice(idx, 1) let removedSwatch = swatches.splice(idx, 1)
swatches = swatches swatches = swatches
dispatch("removeswatch", removedSwatch) dispatch("removeswatch", removedSwatch)
localStorage.setItem("cp:recent-colors", JSON.stringify(swatches)) if (swatchesSetFromLocalStore) {
//as could be a swatch not present in local storage
setRecentColors()
}
} }
function applySwatch(color) { function applySwatch(color) {
@ -159,13 +176,14 @@
$: border = v > 90 && s < 5 ? "1px dashed #dedada" : "" $: border = v > 90 && s < 5 ? "1px dashed #dedada" : ""
$: selectedColorStyle = buildStyle({ background: value, border }) $: selectedColorStyle = buildStyle({ background: value, border })
$: shrink = swatches.length > 0 $: hasSwatches = swatches.length > 0
</script> </script>
<Portal> <Portal>
<div <div
class="colorpicker-container" class="colorpicker-container"
use:keyevents={{"Escape": handleEscape}} use:keyevents={{ Escape: handleEscape }}
transition:fade transition:fade
bind:this={colorPicker} bind:this={colorPicker}
{style} {style}
@ -181,7 +199,10 @@
<div class="alpha-hue-panel"> <div class="alpha-hue-panel">
<div> <div>
<CheckedBackground borderRadius="50%" backgroundSize="8px"> <CheckedBackground borderRadius="50%" backgroundSize="8px">
<div class="selected-color" style={selectedColorStyle} /> <div
class="selected-color"
title={value}
style={selectedColorStyle} />
</CheckedBackground> </CheckedBackground>
</div> </div>
<div> <div>
@ -204,22 +225,24 @@
{#if !disableSwatches} {#if !disableSwatches}
<div transition:fade class="swatch-panel"> <div transition:fade class="swatch-panel">
{#if swatches.length > 0} {#if hasSwatches}
{#each swatches as color, idx} {#each swatches as color, idx}
<Swatch {#if idx < 12}
{color} <Swatch
on:click={() => applySwatch(color)} {color}
on:removeswatch={() => removeSwatch(idx)} /> on:click={() => applySwatch(color)}
on:removeswatch={() => removeSwatch(idx)} />
{/if}
{/each} {/each}
{/if} {/if}
{#if swatches.length !== 12} {#if swatches.length < 12}
<div <div
tabindex="0" tabindex="0"
use:keyevents={{"Enter": addSwatch}} use:keyevents={{ Enter: addSwatch }}
bind:this={adder} bind:this={adder}
transition:fade transition:fade
class="adder" class="adder"
class:shrink class:shrink={hasSwatches}
on:click={addSwatch}> on:click={addSwatch}>
<span>&plus;</span> <span>&plus;</span>
</div> </div>
@ -309,7 +332,7 @@
margin-left: 5px; margin-left: 5px;
margin-top: 3px; margin-top: 3px;
font-weight: 500; font-weight: 500;
outline-color: #003cb0; outline-color: #003cb0;
outline-width: thin; outline-width: thin;
} }

View File

@ -3,7 +3,7 @@
import CheckedBackground from "./CheckedBackground.svelte" import CheckedBackground from "./CheckedBackground.svelte"
import { createEventDispatcher, beforeUpdate, onMount } from "svelte" import { createEventDispatcher, beforeUpdate, onMount } from "svelte"
import {buildStyle, debounce} from "../helpers.js" import { buildStyle, debounce } from "../helpers.js"
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
import { getColorFormat } from "../utils.js" import { getColorFormat } from "../utils.js"
@ -50,7 +50,7 @@
} }
function calculateDimensions() { function calculateDimensions() {
const { const {
top: spaceAbove, top: spaceAbove,
width, width,
bottom, bottom,
@ -71,12 +71,12 @@
y = bottom y = bottom
} }
x = (left + previewCenter) - (220 / 2) x = left + previewCenter - 220 / 2
dimensions = { [positionSide]: y.toFixed(1), left: x.toFixed(1) } dimensions = { [positionSide]: y.toFixed(1), left: x.toFixed(1) }
} }
$: if(open && colorPreview) { $: if (open && colorPreview) {
calculateDimensions() calculateDimensions()
} }
@ -97,6 +97,7 @@
bind:this={colorPreview} bind:this={colorPreview}
bind:clientHeight={previewHeight} bind:clientHeight={previewHeight}
bind:clientWidth={previewWidth} bind:clientWidth={previewWidth}
title={value}
class="color-preview" class="color-preview"
style={previewStyle} style={previewStyle}
on:click={openColorpicker} /> on:click={openColorpicker} />
@ -118,7 +119,10 @@
<div on:click|self={() => (open = false)} class="overlay" /> <div on:click|self={() => (open = false)} class="overlay" />
{/if} {/if}
{:else} {:else}
<div class="color-preview preview-error" style={errorPreviewStyle}> <div
class="color-preview preview-error"
title="Invalid Color"
style={errorPreviewStyle}>
<span>&times;</span> <span>&times;</span>
</div> </div>
{/if} {/if}

View File

@ -2,7 +2,7 @@
import { createEventDispatcher } from "svelte" import { createEventDispatcher } from "svelte"
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
import CheckedBackground from "./CheckedBackground.svelte" import CheckedBackground from "./CheckedBackground.svelte"
import {keyevents} from "../actions" import { keyevents } from "../actions"
export let hovered = false export let hovered = false
export let color = "#fff" export let color = "#fff"
@ -14,8 +14,9 @@
<CheckedBackground borderRadius="6px"> <CheckedBackground borderRadius="6px">
<div <div
tabindex="0" tabindex="0"
use:keyevents={{"Enter": () => dispatch("click")}} use:keyevents={{ Enter: () => dispatch('click') }}
in:fade in:fade
title={color}
class="swatch" class="swatch"
style={`background: ${color};`} style={`background: ${color};`}
on:click|self on:click|self
@ -41,7 +42,7 @@
border: 1px solid #dedada; border: 1px solid #dedada;
height: 20px; height: 20px;
width: 20px; width: 20px;
outline-color: #003cb0; outline-color: #003cb0;
outline-width: thin; outline-width: thin;
} }