UNPKG

hongluan-ui

Version:
444 lines (439 loc) 16.5 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var vue = require('vue'); var lodashUnified = require('lodash-unified'); var index$8 = require('../../button/index.js'); var index$a = require('../../icon/index.js'); var index$6 = require('../../group/index.js'); require('../../../directives/index.js'); require('../../../hooks/index.js'); var index$5 = require('../../tooltip/index.js'); var index$7 = require('../../input/index.js'); require('../../../constants/index.js'); require('../../../utils/index.js'); require('../../system-icon/index.js'); var alphaSlider = require('./components/alpha-slider.js'); var hueSlider = require('./components/hue-slider.js'); var predefine = require('./components/predefine.js'); var svPanel = require('./components/sv-panel.js'); var color = require('./utils/color.js'); var colorPicker = require('./color-picker2.js'); var index = require('../../../hooks/use-locale/index.js'); var index$1 = require('../../../hooks/use-namespace/index.js'); var index$2 = require('../../../hooks/use-form-item/index.js'); var index$3 = require('../../../hooks/use-focus-controller/index.js'); var index$4 = require('../../../hooks/use-consistent-prop/index.js'); var event = require('../../../constants/event.js'); var error = require('../../../utils/error.js'); var aria = require('../../../constants/aria.js'); var index$9 = require('../../../directives/click-outside/index.js'); var arrowDown = require('../../system-icon/src/arrow-down.js'); var close = require('../../system-icon/src/close.js'); const __default__ = vue.defineComponent({ name: "ColorPicker" }); const _sfc_main = /* @__PURE__ */ vue.defineComponent({ ...__default__, props: colorPicker.colorPickerProps, emits: colorPicker.colorPickerEmits, setup(__props, { expose, emit }) { const props = __props; const { t } = index.useLocale(); const { namespace } = index$1.useNamespace("color"); const { form, formItem } = index$2.useFormItem(); const { inputId: buttonId, isLabeledByFormItem } = index$2.useFormItemInputId(props, { formItemContext: formItem }); const hue = vue.ref(); const svPanel$1 = vue.ref(); const alpha = vue.ref(); const popper = vue.ref(); const triggerRef = vue.ref(); const inputRef = vue.ref(); const { isFocused, handleFocus: _handleFocus, handleBlur } = index$3.useFocusController(triggerRef, { beforeBlur(event) { var _a; return (_a = popper.value) == null ? void 0 : _a.isFocusInsideContent(event); }, afterBlur() { setShowPicker(false); resetColor(); } }); const handleFocus = (event) => { if (colorDisabled.value) return blur(); _handleFocus(event); }; let shouldActiveChange = true; const color$1 = vue.reactive(new color["default"]({ enableAlpha: props.showAlpha, format: props.colorFormat || "", value: props.modelValue })); const showPicker = vue.ref(false); const showPanelColor = vue.ref(false); const customInput = vue.ref(""); const displayedColor = vue.computed(() => { if (!props.modelValue && !showPanelColor.value) { return "transparent"; } return displayedRgb(color$1, props.showAlpha); }); const { size: colorSize } = index$4.useConsistentProp(); const colorDisabled = vue.computed(() => { return !!(props.disabled || (form == null ? void 0 : form.disabled)); }); const currentColor = vue.computed(() => { return !props.modelValue && !showPanelColor.value ? "" : color$1.value; }); const popperPaneRef = vue.computed(() => { var _a, _b; return (_b = (_a = popper.value) == null ? void 0 : _a.popperRef) == null ? void 0 : _b.contentRef; }); const buttonAriaLabel = vue.computed(() => { return !isLabeledByFormItem.value ? props.label || props.ariaLabel || t("hl.colorpicker.defaultLabel") : void 0; }); const buttonAriaLabelledby = vue.computed(() => { return isLabeledByFormItem.value ? formItem == null ? void 0 : formItem.labelId : void 0; }); function displayedRgb(color2, showAlpha) { if (!(color2 instanceof color["default"])) { throw new TypeError("color should be instance of _color Class"); } const { r, g, b } = color2.toRgb(); return showAlpha ? `rgba(${r}, ${g}, ${b}, ${color2.get("alpha") / 100})` : `rgb(${r}, ${g}, ${b})`; } function setShowPicker(value) { showPicker.value = value; } const debounceSetShowPicker = lodashUnified.debounce(setShowPicker, 100, { leading: true }); function show() { if (colorDisabled.value) return; setShowPicker(true); } function hide() { debounceSetShowPicker(false); resetColor(); } function resetColor() { vue.nextTick(() => { if (props.modelValue) { color$1.fromString(props.modelValue); } else { color$1.value = ""; vue.nextTick(() => { showPanelColor.value = false; }); } }); } function handleTrigger() { if (colorDisabled.value) return; debounceSetShowPicker(!showPicker.value); } function handleConfirm() { color$1.fromString(customInput.value); } function confirmValue() { var _a; const value = color$1.value; emit(event.UPDATE_MODEL_EVENT, value); emit("change", value); if (props.validateEvent) { (_a = formItem == null ? void 0 : formItem.validate) == null ? void 0 : _a.call(formItem, "change").catch((err) => error.debugWarn(err)); } debounceSetShowPicker(false); vue.nextTick(() => { const newColor = new color["default"]({ enableAlpha: props.showAlpha, format: props.colorFormat || "", value: props.modelValue }); if (!color$1.compare(newColor)) { resetColor(); } }); } function clear() { var _a; debounceSetShowPicker(false); emit(event.UPDATE_MODEL_EVENT, null); emit("change", null); if (props.modelValue !== null && props.validateEvent) { (_a = formItem == null ? void 0 : formItem.validate) == null ? void 0 : _a.call(formItem, "change").catch((err) => error.debugWarn(err)); } resetColor(); } function handleClickOutside(event) { if (!showPicker.value) return; hide(); if (isFocused.value) { const _event = new FocusEvent("focus", event); handleBlur(_event); } } function handleEsc(event) { event.preventDefault(); event.stopPropagation(); setShowPicker(false); resetColor(); } function handleKeyDown(event) { switch (event.code) { case aria.EVENT_CODE.enter: case aria.EVENT_CODE.space: event.preventDefault(); event.stopPropagation(); show(); inputRef.value.focus(); break; case aria.EVENT_CODE.esc: handleEsc(event); break; } } function focus() { triggerRef.value.focus(); } function blur() { triggerRef.value.blur(); } vue.onMounted(() => { if (props.modelValue) { customInput.value = currentColor.value; } }); vue.watch(() => props.modelValue, (newVal) => { if (!newVal) { showPanelColor.value = false; } else if (newVal && newVal !== color$1.value) { shouldActiveChange = false; color$1.fromString(newVal); } }); vue.watch(() => currentColor.value, (val) => { customInput.value = val; shouldActiveChange && emit("activeChange", val); shouldActiveChange = true; }); vue.watch(() => color$1.value, () => { if (!props.modelValue && !showPanelColor.value) { showPanelColor.value = true; } }); vue.watch(() => showPicker.value, () => { vue.nextTick(() => { var _a, _b, _c; (_a = hue.value) == null ? void 0 : _a.update(); (_b = svPanel$1.value) == null ? void 0 : _b.update(); (_c = alpha.value) == null ? void 0 : _c.update(); }); }); vue.provide(colorPicker.colorPickerContextKey, { currentColor }); expose({ color: color$1, popperPaneRef, show, hide, focus, blur }); return (_ctx, _cache) => { return vue.openBlock(), vue.createBlock(vue.unref(index$5.HlTooltip), { ref_key: "popper", ref: popper, visible: showPicker.value, "show-arrow": false, "fallback-placements": ["bottom", "top", "right", "left"], offset: _ctx.popperOffset, "gpu-acceleration": false, "popper-class": [vue.unref(namespace) + "-picker-popper", _ctx.popperClass], "stop-popper-mouse-event": false, teleported: _ctx.teleported, trigger: "click", transition: "dropdown", persistent: "", onHide: ($event) => setShowPicker(false) }, { content: vue.withCtx(() => [ vue.withDirectives((vue.openBlock(), vue.createBlock(vue.unref(index$6.HlGroup), { class: vue.normalizeClass(vue.unref(namespace) + "-panel"), dir: "vertical", full: "full-x", gap: "var(--xs)", onKeydown: vue.withKeys(handleEsc, ["esc"]) }, { default: vue.withCtx(() => [ vue.createVNode(svPanel["default"], { ref_key: "svPanel", ref: svPanel$1, color: vue.unref(color$1) }, null, 8, ["color"]), vue.createVNode(vue.unref(index$6.HlGroup), { gap: "var(--xs)" }, { default: vue.withCtx(() => [ vue.createVNode(vue.unref(index$6.HlGroup), { dir: "vertical", full: "full-x", gap: "var(--xxs)" }, { default: vue.withCtx(() => [ vue.createVNode(hueSlider["default"], { ref_key: "hue", ref: hue, color: vue.unref(color$1) }, null, 8, ["color"]), _ctx.showAlpha ? (vue.openBlock(), vue.createBlock(alphaSlider["default"], { key: 0, ref_key: "alpha", ref: alpha, color: vue.unref(color$1) }, null, 8, ["color"])) : vue.createCommentVNode("v-if", true) ]), _: 1 }), _ctx.showAlpha ? (vue.openBlock(), vue.createElementBlock("div", { key: 0, class: "current-color", style: vue.normalizeStyle(`background-color: ${customInput.value}`) }, null, 4)) : vue.createCommentVNode("v-if", true) ]), _: 1 }), _ctx.predefine ? (vue.openBlock(), vue.createBlock(predefine["default"], { key: 0, ref: "predefine", "enable-alpha": _ctx.showAlpha, color: vue.unref(color$1), colors: _ctx.predefine, class: "m-t-xs" }, null, 8, ["enable-alpha", "color", "colors"])) : vue.createCommentVNode("v-if", true), vue.createVNode(vue.unref(index$6.HlGroup), { align: "items-between", gap: "var(--xs)", class: "m-t-xs" }, { default: vue.withCtx(() => [ vue.createVNode(vue.unref(index$7.HlInput), { ref_key: "inputRef", ref: inputRef, modelValue: customInput.value, "onUpdate:modelValue": ($event) => customInput.value = $event, "validate-event": false, size: "sm", fill: "", block: "", spellcheck: "false", onKeyup: vue.withKeys(handleConfirm, ["enter"]), onBlur: handleConfirm }, null, 8, ["modelValue", "onUpdate:modelValue", "onKeyup"]), vue.createVNode(vue.unref(index$6.HlGroup), { class: "static" }, { default: vue.withCtx(() => [ vue.createVNode(vue.unref(index$8.HlButton), { size: "sm", type: "link", onClick: clear }, { default: vue.withCtx(() => [ vue.createTextVNode(vue.toDisplayString(vue.unref(t)("hl.colorpicker.clear")), 1) ]), _: 1 }), vue.createVNode(vue.unref(index$8.HlButton), { type: "primary", size: "sm", onClick: confirmValue }, { default: vue.withCtx(() => [ vue.createTextVNode(vue.toDisplayString(vue.unref(t)("hl.colorpicker.confirm")), 1) ]), _: 1 }) ]), _: 1 }) ]), _: 1 }) ]), _: 1 }, 8, ["class", "onKeydown"])), [ [vue.unref(index$9["default"]), handleClickOutside] ]) ]), default: vue.withCtx(() => [ vue.createElementVNode("div", vue.mergeProps({ id: vue.unref(buttonId), ref_key: "triggerRef", ref: triggerRef }, _ctx.$attrs, { class: [ vue.unref(namespace) + "-picker", vue.unref(colorDisabled) ? "is-disabled" : "", vue.unref(isFocused) ? "is-focused" : "", vue.unref(colorSize) ], role: "button", "aria-label": vue.unref(buttonAriaLabel), "aria-labelledby": vue.unref(buttonAriaLabelledby), "aria-description": vue.unref(t)("hl.colorpicker.description", { color: _ctx.modelValue || "" }), "aria-disabled": vue.unref(colorDisabled), tabindex: vue.unref(colorDisabled) ? -1 : _ctx.tabindex, onKeydown: handleKeyDown, onFocus: handleFocus, onBlur: vue.unref(handleBlur) }), [ vue.unref(colorDisabled) ? (vue.openBlock(), vue.createElementBlock("div", { key: 0, class: "picker-mask" })) : vue.createCommentVNode("v-if", true), vue.createElementVNode("div", { class: "picker-trigger", onClick: handleTrigger }, [ vue.createElementVNode("span", { class: "picker-color", style: vue.normalizeStyle({ backgroundColor: vue.unref(displayedColor) }) }, [ vue.withDirectives(vue.createVNode(vue.unref(index$a.HlIcon), { class: vue.normalizeClass(["picker-icon"]) }, { default: vue.withCtx(() => [ vue.createVNode(vue.unref(arrowDown["default"])) ]), _: 1 }, 512), [ [vue.vShow, _ctx.modelValue || showPanelColor.value] ]), vue.withDirectives(vue.createVNode(vue.unref(index$a.HlIcon), { class: vue.normalizeClass(["picker-empty", "icon-close"]) }, { default: vue.withCtx(() => [ vue.createVNode(vue.unref(close["default"])) ]), _: 1 }, 512), [ [vue.vShow, !_ctx.modelValue && !showPanelColor.value] ]) ], 4) ]) ], 16, ["id", "aria-label", "aria-labelledby", "aria-description", "aria-disabled", "tabindex", "onBlur"]) ]), _: 1 }, 8, ["visible", "offset", "popper-class", "teleported", "onHide"]); }; } }); exports["default"] = _sfc_main; //# sourceMappingURL=color-picker.js.map