hongluan-ui
Version:
Hongluan Component Library for Vue 3
325 lines (322 loc) • 12.8 kB
JavaScript
import { defineComponent, getCurrentInstance, ref, computed, watch, onBeforeUnmount, unref, provide, toRef, resolveComponent, openBlock, createElementBlock, normalizeClass, createVNode, createSlots, withCtx, renderSlot, createBlock, Fragment, mergeProps, createElementVNode, createCommentVNode } from 'vue';
import { HlButton } from '../../button/index.mjs';
import { HlTooltip } from '../../tooltip/index.mjs';
import { HlGroup } from '../../group/index.mjs';
import { HlScrollbar } from '../../scrollbar/index.mjs';
import '../../roving-focus-group/index.mjs';
import '../../slot/index.mjs';
import '../../popper/index.mjs';
import '../../../utils/index.mjs';
import '../../../constants/index.mjs';
import '../../../hooks/index.mjs';
import { HlCollection, dropdownProps } from './dropdown2.mjs';
import { DROPDOWN_INJECTION_KEY } from './tokens.mjs';
import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
import { OnlyChild } from '../../slot/src/only-child.mjs';
import HlRovingFocusGroup from '../../roving-focus-group/src/roving-focus-group2.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { useLocale } from '../../../hooks/use-locale/index.mjs';
import { EVENT_CODE } from '../../../constants/aria.mjs';
import { useDeprecateAppendToBody } from '../../popper/src/deprecation.mjs';
import { addUnit } from '../../../utils/dom/style.mjs';
import { castArray } from 'lodash-unified';
import { useId } from '../../../hooks/use-id/index.mjs';
import { useConsistentProp } from '../../../hooks/use-consistent-prop/index.mjs';
const _sfc_main = defineComponent({
name: "Dropdown",
components: {
HlButton,
HlGroup,
HlScrollbar,
HlOnlyChild: OnlyChild,
HlDropdownCollection: HlCollection,
HlTooltip,
HlRovingFocusGroup
},
props: dropdownProps,
emits: ["visible-change", "click", "command"],
setup(props, { emit }) {
const _instance = getCurrentInstance();
const { namespace } = useNamespace();
const { t } = useLocale();
const triggeringElementRef = ref();
const referenceElementRef = ref();
const popperRef = ref(null);
const contentRef = ref(null);
const scrollbar = ref(null);
const currentTabId = ref(null);
const isUsingKeyboard = ref(false);
const triggerKeys = [EVENT_CODE.enter, EVENT_CODE.space, EVENT_CODE.down];
const { compatTeleported } = useDeprecateAppendToBody("Dropdown", "popperAppendToBody");
const wrapStyle = computed(() => ({
maxHeight: addUnit(props.maxHeight)
}));
const dropdownTriggerKls = computed(() => [dropdownSize.value]);
const trigger = computed(() => castArray(props.trigger));
const defaultTriggerId = useId().value;
const triggerId = computed(() => props.id || defaultTriggerId);
watch([triggeringElementRef, trigger], ([triggeringElement, trigger2], [prevTriggeringElement]) => {
var _a, _b, _c;
if ((_a = prevTriggeringElement == null ? void 0 : prevTriggeringElement.$el) == null ? void 0 : _a.removeEventListener) {
prevTriggeringElement.$el.removeEventListener("pointerenter", onAutofocusTriggerEnter);
}
if ((_b = triggeringElement == null ? void 0 : triggeringElement.$el) == null ? void 0 : _b.removeEventListener) {
triggeringElement.$el.removeEventListener("pointerenter", onAutofocusTriggerEnter);
}
if (((_c = triggeringElement == null ? void 0 : triggeringElement.$el) == null ? void 0 : _c.addEventListener) && trigger2.includes("hover")) {
triggeringElement.$el.addEventListener("pointerenter", onAutofocusTriggerEnter);
}
}, { immediate: true });
onBeforeUnmount(() => {
var _a, _b;
if ((_b = (_a = triggeringElementRef.value) == null ? void 0 : _a.$el) == null ? void 0 : _b.removeEventListener) {
triggeringElementRef.value.$el.removeEventListener("pointerenter", onAutofocusTriggerEnter);
}
});
function handleClick() {
handleClose();
}
function handleClose() {
var _a;
(_a = popperRef.value) == null ? void 0 : _a.onClose();
}
function handleOpen() {
var _a;
(_a = popperRef.value) == null ? void 0 : _a.onOpen();
}
const { size: dropdownSize } = useConsistentProp();
function commandHandler(...args) {
emit("command", ...args);
}
function onAutofocusTriggerEnter() {
var _a, _b;
(_b = (_a = triggeringElementRef.value) == null ? void 0 : _a.$el) == null ? void 0 : _b.focus();
}
function onItemEnter() {
}
function onItemLeave() {
const contentEl = unref(contentRef);
trigger.value.includes("hover") && (contentEl == null ? void 0 : contentEl.focus());
currentTabId.value = null;
}
function handleCurrentTabIdChange(id) {
currentTabId.value = id;
}
function handleEntryFocus(e) {
if (!isUsingKeyboard.value) {
e.preventDefault();
e.stopImmediatePropagation();
}
}
function handleBeforeShowTooltip() {
emit("visible-change", true);
}
function handleShowTooltip(event) {
if ((event == null ? void 0 : event.type) === "keydown") {
contentRef.value.focus();
}
}
function handleBeforeHideTooltip() {
emit("visible-change", false);
}
provide(DROPDOWN_INJECTION_KEY, {
contentRef,
role: computed(() => props.role),
triggerId,
isUsingKeyboard,
onItemEnter,
onItemLeave
});
provide("hlDropdown", {
instance: _instance,
dropdownSize,
handleClick,
commandHandler,
trigger: toRef(props, "trigger"),
hideOnClick: toRef(props, "hideOnClick")
});
const onFocusAfterTrapped = (e) => {
var _a, _b;
e.preventDefault();
(_b = (_a = contentRef.value) == null ? void 0 : _a.focus) == null ? void 0 : _b.call(_a, {
preventScroll: true
});
};
const handlerMainButtonClick = (event) => {
emit("click", event);
};
return {
t,
namespace,
scrollbar,
wrapStyle,
dropdownTriggerKls,
dropdownSize,
triggerId,
triggerKeys,
currentTabId,
compatTeleported,
handleCurrentTabIdChange,
handlerMainButtonClick,
handleEntryFocus,
handleClose,
handleOpen,
handleBeforeShowTooltip,
handleShowTooltip,
handleBeforeHideTooltip,
onFocusAfterTrapped,
popperRef,
contentRef,
triggeringElementRef,
referenceElementRef
};
}
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
var _a;
const _component_hl_dropdown_collection = resolveComponent("hl-dropdown-collection");
const _component_hl_roving_focus_group = resolveComponent("hl-roving-focus-group");
const _component_hl_scrollbar = resolveComponent("hl-scrollbar");
const _component_hl_only_child = resolveComponent("hl-only-child");
const _component_hl_tooltip = resolveComponent("hl-tooltip");
const _component_hl_button = resolveComponent("hl-button");
const _component_hl_group = resolveComponent("hl-group");
return openBlock(), createElementBlock("div", {
class: normalizeClass([_ctx.namespace + "-dropdown", _ctx.disabled ? "is-disabled" : ""])
}, [
createVNode(_component_hl_tooltip, {
ref: "popperRef",
role: _ctx.role,
effect: _ctx.effect,
offset: _ctx.popperOffset,
"fallback-placements": ["bottom", "top"],
"popper-options": _ctx.popperOptions,
"gpu-acceleration": false,
"hide-after": _ctx.trigger === "hover" ? _ctx.hideTimeout : 0,
"manual-mode": true,
placement: _ctx.placement,
"popper-class": [`${_ctx.namespace}-dropdown-popper`, _ctx.popperClass],
"reference-element": (_a = _ctx.referenceElementRef) == null ? void 0 : _a.$el,
trigger: _ctx.trigger,
"trigger-keys": _ctx.triggerKeys,
"trigger-target-el": _ctx.contentRef,
"show-after": _ctx.trigger === "hover" ? _ctx.showTimeout : 0,
"stop-popper-mouse-event": false,
"virtual-ref": _ctx.triggeringElementRef,
"virtual-triggering": !!_ctx.split,
disabled: _ctx.disabled,
teleported: _ctx.compatTeleported,
transition: "dropdown",
"show-arrow": false,
persistent: "",
onBeforeShow: _ctx.handleBeforeShowTooltip,
onShow: _ctx.handleShowTooltip,
onBeforeHide: _ctx.handleBeforeHideTooltip
}, createSlots({
content: withCtx(() => [
createVNode(_component_hl_scrollbar, {
ref: "scrollbar",
"wrap-style": _ctx.wrapStyle,
tag: "div",
"view-class": "dropdown-list"
}, {
default: withCtx(() => [
createVNode(_component_hl_roving_focus_group, {
loop: _ctx.loop,
"current-tab-id": _ctx.currentTabId,
orientation: "horizontal",
onCurrentTabIdChange: _ctx.handleCurrentTabIdChange,
onEntryFocus: _ctx.handleEntryFocus
}, {
default: withCtx(() => [
createVNode(_component_hl_dropdown_collection, null, {
default: withCtx(() => [
renderSlot(_ctx.$slots, "dropdown")
]),
_: 3
})
]),
_: 3
}, 8, ["loop", "current-tab-id", "onCurrentTabIdChange", "onEntryFocus"])
]),
_: 3
}, 8, ["wrap-style"])
]),
_: 2
}, [
!_ctx.split ? {
name: "default",
fn: withCtx(() => [
createVNode(_component_hl_only_child, {
id: _ctx.triggerId,
ref: "triggeringElementRef",
role: "button",
tabindex: _ctx.tabindex
}, {
default: withCtx(() => [
renderSlot(_ctx.$slots, "default")
]),
_: 3
}, 8, ["id", "tabindex"])
])
} : void 0
]), 1032, ["role", "effect", "offset", "popper-options", "hide-after", "placement", "popper-class", "reference-element", "trigger", "trigger-keys", "trigger-target-el", "show-after", "virtual-ref", "virtual-triggering", "disabled", "teleported", "onBeforeShow", "onShow", "onBeforeHide"]),
_ctx.split ? (openBlock(), createBlock(_component_hl_group, {
key: 0,
class: normalizeClass([_ctx.dropdownTriggerKls, "dropdown-button"])
}, {
default: withCtx(() => [
_ctx.split === "button" ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [
createVNode(_component_hl_button, mergeProps({ ref: "referenceElementRef" }, _ctx.buttonProps, {
size: _ctx.dropdownSize,
type: _ctx.type,
disabled: _ctx.disabled,
tabindex: _ctx.tabindex,
onClick: _ctx.handlerMainButtonClick
}), {
default: withCtx(() => [
renderSlot(_ctx.$slots, "default")
]),
_: 3
}, 16, ["size", "type", "disabled", "tabindex", "onClick"]),
createVNode(_component_hl_button, mergeProps({
id: _ctx.triggerId,
ref: "triggeringElementRef"
}, _ctx.buttonProps, {
role: "button",
class: ["caret-button", _ctx.type ? "is-active" : ""],
size: _ctx.dropdownSize,
type: _ctx.type,
disabled: _ctx.disabled,
tabindex: _ctx.tabindex,
"aria-label": _ctx.t("hl.dropdown.toggleDropdown"),
equal: ""
}), {
default: withCtx(() => [
createElementVNode("span", { class: "caret" })
]),
_: 1
}, 16, ["id", "class", "size", "type", "disabled", "tabindex", "aria-label"])
], 64)) : _ctx.split === "link" ? (openBlock(), createElementBlock(Fragment, { key: 1 }, [
createElementVNode("span", {
ref: "referenceElementRef",
class: normalizeClass(["text-" + _ctx.type, _ctx.dropdownSize, _ctx.disabled ? "is-disabled" : ""]),
style: { "cursor": "pointer" },
onClick: () => !_ctx.disabled && _ctx.handlerMainButtonClick()
}, [
renderSlot(_ctx.$slots, "default")
], 10, ["onClick"]),
createElementVNode("span", {
ref: "triggeringElementRef",
class: normalizeClass(["text-" + _ctx.type, _ctx.dropdownSize, _ctx.disabled ? "is-disabled" : "", "caret"]),
style: { "cursor": "pointer" }
}, null, 2)
], 64)) : createCommentVNode("v-if", true)
]),
_: 3
}, 8, ["class"])) : createCommentVNode("v-if", true)
], 2);
}
var Dropdown = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export { Dropdown as default };
//# sourceMappingURL=dropdown.mjs.map