hongluan-ui
Version:
Hongluan Component Library for Vue 3
440 lines (437 loc) • 16 kB
JavaScript
import { defineComponent, ref, reactive, computed, nextTick, onMounted, watch, provide, openBlock, createBlock, unref, withCtx, withDirectives, normalizeClass, withKeys, createVNode, createCommentVNode, createElementBlock, normalizeStyle, createTextVNode, toDisplayString, createElementVNode, mergeProps, vShow } from 'vue';
import { debounce } from 'lodash-unified';
import { HlButton } from '../../button/index.mjs';
import { HlIcon } from '../../icon/index.mjs';
import { HlGroup } from '../../group/index.mjs';
import '../../../directives/index.mjs';
import '../../../hooks/index.mjs';
import { HlTooltip } from '../../tooltip/index.mjs';
import { HlInput } from '../../input/index.mjs';
import '../../../constants/index.mjs';
import '../../../utils/index.mjs';
import '../../system-icon/index.mjs';
import _sfc_main$1 from './components/alpha-slider.mjs';
import HueSlider from './components/hue-slider.mjs';
import Predefine from './components/predefine.mjs';
import SvPanel from './components/sv-panel.mjs';
import Color from './utils/color.mjs';
import { colorPickerProps, colorPickerEmits, colorPickerContextKey } from './color-picker2.mjs';
import { useLocale } from '../../../hooks/use-locale/index.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { useFormItem, useFormItemInputId } from '../../../hooks/use-form-item/index.mjs';
import { useFocusController } from '../../../hooks/use-focus-controller/index.mjs';
import { useConsistentProp } from '../../../hooks/use-consistent-prop/index.mjs';
import { UPDATE_MODEL_EVENT } from '../../../constants/event.mjs';
import { debugWarn } from '../../../utils/error.mjs';
import { EVENT_CODE } from '../../../constants/aria.mjs';
import ClickOutside from '../../../directives/click-outside/index.mjs';
import SystemArrowDown from '../../system-icon/src/arrow-down.mjs';
import SystemClose from '../../system-icon/src/close.mjs';
const __default__ = defineComponent({
name: "ColorPicker"
});
const _sfc_main = /* @__PURE__ */ defineComponent({
...__default__,
props: colorPickerProps,
emits: colorPickerEmits,
setup(__props, { expose, emit }) {
const props = __props;
const { t } = useLocale();
const { namespace } = useNamespace("color");
const { form, formItem } = useFormItem();
const { inputId: buttonId, isLabeledByFormItem } = useFormItemInputId(props, {
formItemContext: formItem
});
const hue = ref();
const svPanel = ref();
const alpha = ref();
const popper = ref();
const triggerRef = ref();
const inputRef = ref();
const {
isFocused,
handleFocus: _handleFocus,
handleBlur
} = 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 = reactive(new Color({
enableAlpha: props.showAlpha,
format: props.colorFormat || "",
value: props.modelValue
}));
const showPicker = ref(false);
const showPanelColor = ref(false);
const customInput = ref("");
const displayedColor = computed(() => {
if (!props.modelValue && !showPanelColor.value) {
return "transparent";
}
return displayedRgb(color, props.showAlpha);
});
const { size: colorSize } = useConsistentProp();
const colorDisabled = computed(() => {
return !!(props.disabled || (form == null ? void 0 : form.disabled));
});
const currentColor = computed(() => {
return !props.modelValue && !showPanelColor.value ? "" : color.value;
});
const popperPaneRef = computed(() => {
var _a, _b;
return (_b = (_a = popper.value) == null ? void 0 : _a.popperRef) == null ? void 0 : _b.contentRef;
});
const buttonAriaLabel = computed(() => {
return !isLabeledByFormItem.value ? props.label || props.ariaLabel || t("hl.colorpicker.defaultLabel") : void 0;
});
const buttonAriaLabelledby = computed(() => {
return isLabeledByFormItem.value ? formItem == null ? void 0 : formItem.labelId : void 0;
});
function displayedRgb(color2, showAlpha) {
if (!(color2 instanceof Color)) {
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 = debounce(setShowPicker, 100, { leading: true });
function show() {
if (colorDisabled.value)
return;
setShowPicker(true);
}
function hide() {
debounceSetShowPicker(false);
resetColor();
}
function resetColor() {
nextTick(() => {
if (props.modelValue) {
color.fromString(props.modelValue);
} else {
color.value = "";
nextTick(() => {
showPanelColor.value = false;
});
}
});
}
function handleTrigger() {
if (colorDisabled.value)
return;
debounceSetShowPicker(!showPicker.value);
}
function handleConfirm() {
color.fromString(customInput.value);
}
function confirmValue() {
var _a;
const value = color.value;
emit(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) => debugWarn(err));
}
debounceSetShowPicker(false);
nextTick(() => {
const newColor = new Color({
enableAlpha: props.showAlpha,
format: props.colorFormat || "",
value: props.modelValue
});
if (!color.compare(newColor)) {
resetColor();
}
});
}
function clear() {
var _a;
debounceSetShowPicker(false);
emit(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) => 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 EVENT_CODE.enter:
case EVENT_CODE.space:
event.preventDefault();
event.stopPropagation();
show();
inputRef.value.focus();
break;
case EVENT_CODE.esc:
handleEsc(event);
break;
}
}
function focus() {
triggerRef.value.focus();
}
function blur() {
triggerRef.value.blur();
}
onMounted(() => {
if (props.modelValue) {
customInput.value = currentColor.value;
}
});
watch(() => props.modelValue, (newVal) => {
if (!newVal) {
showPanelColor.value = false;
} else if (newVal && newVal !== color.value) {
shouldActiveChange = false;
color.fromString(newVal);
}
});
watch(() => currentColor.value, (val) => {
customInput.value = val;
shouldActiveChange && emit("activeChange", val);
shouldActiveChange = true;
});
watch(() => color.value, () => {
if (!props.modelValue && !showPanelColor.value) {
showPanelColor.value = true;
}
});
watch(() => showPicker.value, () => {
nextTick(() => {
var _a, _b, _c;
(_a = hue.value) == null ? void 0 : _a.update();
(_b = svPanel.value) == null ? void 0 : _b.update();
(_c = alpha.value) == null ? void 0 : _c.update();
});
});
provide(colorPickerContextKey, {
currentColor
});
expose({
color,
popperPaneRef,
show,
hide,
focus,
blur
});
return (_ctx, _cache) => {
return openBlock(), createBlock(unref(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": [unref(namespace) + "-picker-popper", _ctx.popperClass],
"stop-popper-mouse-event": false,
teleported: _ctx.teleported,
trigger: "click",
transition: "dropdown",
persistent: "",
onHide: ($event) => setShowPicker(false)
}, {
content: withCtx(() => [
withDirectives((openBlock(), createBlock(unref(HlGroup), {
class: normalizeClass(unref(namespace) + "-panel"),
dir: "vertical",
full: "full-x",
gap: "var(--xs)",
onKeydown: withKeys(handleEsc, ["esc"])
}, {
default: withCtx(() => [
createVNode(SvPanel, {
ref_key: "svPanel",
ref: svPanel,
color: unref(color)
}, null, 8, ["color"]),
createVNode(unref(HlGroup), { gap: "var(--xs)" }, {
default: withCtx(() => [
createVNode(unref(HlGroup), {
dir: "vertical",
full: "full-x",
gap: "var(--xxs)"
}, {
default: withCtx(() => [
createVNode(HueSlider, {
ref_key: "hue",
ref: hue,
color: unref(color)
}, null, 8, ["color"]),
_ctx.showAlpha ? (openBlock(), createBlock(_sfc_main$1, {
key: 0,
ref_key: "alpha",
ref: alpha,
color: unref(color)
}, null, 8, ["color"])) : createCommentVNode("v-if", true)
]),
_: 1
}),
_ctx.showAlpha ? (openBlock(), createElementBlock("div", {
key: 0,
class: "current-color",
style: normalizeStyle(`background-color: ${customInput.value}`)
}, null, 4)) : createCommentVNode("v-if", true)
]),
_: 1
}),
_ctx.predefine ? (openBlock(), createBlock(Predefine, {
key: 0,
ref: "predefine",
"enable-alpha": _ctx.showAlpha,
color: unref(color),
colors: _ctx.predefine,
class: "m-t-xs"
}, null, 8, ["enable-alpha", "color", "colors"])) : createCommentVNode("v-if", true),
createVNode(unref(HlGroup), {
align: "items-between",
gap: "var(--xs)",
class: "m-t-xs"
}, {
default: withCtx(() => [
createVNode(unref(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: withKeys(handleConfirm, ["enter"]),
onBlur: handleConfirm
}, null, 8, ["modelValue", "onUpdate:modelValue", "onKeyup"]),
createVNode(unref(HlGroup), { class: "static" }, {
default: withCtx(() => [
createVNode(unref(HlButton), {
size: "sm",
type: "link",
onClick: clear
}, {
default: withCtx(() => [
createTextVNode(toDisplayString(unref(t)("hl.colorpicker.clear")), 1)
]),
_: 1
}),
createVNode(unref(HlButton), {
type: "primary",
size: "sm",
onClick: confirmValue
}, {
default: withCtx(() => [
createTextVNode(toDisplayString(unref(t)("hl.colorpicker.confirm")), 1)
]),
_: 1
})
]),
_: 1
})
]),
_: 1
})
]),
_: 1
}, 8, ["class", "onKeydown"])), [
[unref(ClickOutside), handleClickOutside]
])
]),
default: withCtx(() => [
createElementVNode("div", mergeProps({
id: unref(buttonId),
ref_key: "triggerRef",
ref: triggerRef
}, _ctx.$attrs, {
class: [
unref(namespace) + "-picker",
unref(colorDisabled) ? "is-disabled" : "",
unref(isFocused) ? "is-focused" : "",
unref(colorSize)
],
role: "button",
"aria-label": unref(buttonAriaLabel),
"aria-labelledby": unref(buttonAriaLabelledby),
"aria-description": unref(t)("hl.colorpicker.description", { color: _ctx.modelValue || "" }),
"aria-disabled": unref(colorDisabled),
tabindex: unref(colorDisabled) ? -1 : _ctx.tabindex,
onKeydown: handleKeyDown,
onFocus: handleFocus,
onBlur: unref(handleBlur)
}), [
unref(colorDisabled) ? (openBlock(), createElementBlock("div", {
key: 0,
class: "picker-mask"
})) : createCommentVNode("v-if", true),
createElementVNode("div", {
class: "picker-trigger",
onClick: handleTrigger
}, [
createElementVNode("span", {
class: "picker-color",
style: normalizeStyle({
backgroundColor: unref(displayedColor)
})
}, [
withDirectives(createVNode(unref(HlIcon), {
class: normalizeClass(["picker-icon"])
}, {
default: withCtx(() => [
createVNode(unref(SystemArrowDown))
]),
_: 1
}, 512), [
[vShow, _ctx.modelValue || showPanelColor.value]
]),
withDirectives(createVNode(unref(HlIcon), {
class: normalizeClass(["picker-empty", "icon-close"])
}, {
default: withCtx(() => [
createVNode(unref(SystemClose))
]),
_: 1
}, 512), [
[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"]);
};
}
});
export { _sfc_main as default };
//# sourceMappingURL=color-picker.mjs.map