@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
145 lines (144 loc) • 3.9 kB
JavaScript
import { Component, h, Host, Prop, Watch } from "@stencil/core";
import Color from "color";
import { COLORS, CSS } from "./resources";
export class CalciteColorSwatch {
constructor() {
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/**
* Used to display whether the swatch is active or not.
*/
this.active = false;
/**
* The component scale.
*/
this.scale = "m";
/**
* The component's theme.
*/
this.theme = "light";
}
handleColorChange(color) {
this.internalColor = Color(color);
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
componentWillLoad() {
this.handleColorChange(this.color);
}
render() {
const { active, internalColor, theme } = this;
const borderRadius = active ? "100%" : "0";
const hex = internalColor.hex();
const borderColor = active
? COLORS.activeBorder
: internalColor[theme === "light" ? "darken" : "whiten"](0.25).hex();
return (h(Host, { "aria-label": hex, title: hex },
h("svg", { class: CSS.swatch, xmlns: "http://www.w3.org/2000/svg" },
h("rect", { fill: hex, height: "100%", rx: borderRadius, stroke: borderColor, width: "100%" }))));
}
static get is() { return "calcite-color-swatch"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() { return {
"$": ["calcite-color-swatch.scss"]
}; }
static get styleUrls() { return {
"$": ["calcite-color-swatch.css"]
}; }
static get properties() { return {
"active": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Used to display whether the swatch is active or not."
},
"attribute": "active",
"reflect": true,
"defaultValue": "false"
},
"color": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"text": "https://developer.mozilla.org/en-US/docs/Web/CSS/color_value",
"name": "see"
}],
"text": "The color value."
},
"attribute": "color",
"reflect": false
},
"scale": {
"type": "string",
"mutable": false,
"complexType": {
"original": "Scale",
"resolved": "\"l\" | \"m\" | \"s\"",
"references": {
"Scale": {
"location": "import",
"path": "../interfaces"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "The component scale."
},
"attribute": "scale",
"reflect": true,
"defaultValue": "\"m\""
},
"theme": {
"type": "string",
"mutable": false,
"complexType": {
"original": "Theme",
"resolved": "\"dark\" | \"light\"",
"references": {
"Theme": {
"location": "import",
"path": "../interfaces"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "The component's theme."
},
"attribute": "theme",
"reflect": true,
"defaultValue": "\"light\""
}
}; }
static get watchers() { return [{
"propName": "color",
"methodName": "handleColorChange"
}]; }
}