primevue
Version:
PrimeVue is a premium UI library for Vue featuring a rich set of 90+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with PrimeBlock, wh
382 lines (367 loc) • 12.1 kB
JavaScript
import { mergeProps, openBlock, createBlock, resolveDynamicComponent, withCtx, renderSlot, normalizeClass } from 'vue';
import BaseComponent from '@primevue/core/basecomponent';
import SidebarStyle from 'primevue/sidebar/style';
import { focus } from '@primeuix/utils/dom';
var script$1 = {
name: 'BaseSidebar',
"extends": BaseComponent,
props: {
id: {
type: String,
"default": undefined
},
side: {
type: String,
"default": 'left'
},
variant: {
type: String,
"default": 'sidebar'
},
collapsible: {
type: String,
"default": 'icon'
},
overlay: {
type: Boolean,
"default": false
},
open: {
type: Boolean,
"default": undefined
},
openOnHover: {
type: Boolean,
"default": false
},
hoverOpenDelay: {
type: Number,
"default": 50
},
hoverCloseDelay: {
type: Number,
"default": 100
},
dismissable: {
type: Boolean,
"default": true
},
hideOnOutsideClick: {
type: Boolean,
"default": true
},
width: {
type: String,
"default": '16rem'
},
iconWidth: {
type: String,
"default": '3rem'
},
as: {
type: [String, Object],
"default": 'DIV'
},
asChild: {
type: Boolean,
"default": false
}
},
style: SidebarStyle,
provide: function provide() {
return {
$pcSidebar: this,
$parentInstance: this
};
}
};
// Always-on tree keyboard navigation, scoped to the whole sidebar (root-level, read from DOM).
var MENU_ITEM_SELECTOR = '[data-menu-item]';
var isMenuItemDisabled = function isMenuItemDisabled(el) {
return el.hasAttribute('disabled') || el.getAttribute('aria-disabled') === 'true';
};
// branch = collapsible item
var isMenuItemBranch = function isMenuItemBranch(el) {
return el.hasAttribute('data-state');
};
var isMenuItemExpanded = function isMenuItemExpanded(el) {
return el.getAttribute('data-state') === 'expanded';
};
// visible (not under a collapsed ancestor)
var isMenuItemReachable = function isMenuItemReachable(el, scope) {
var node = el;
while (node && node !== scope) {
if (node.getAttribute('aria-hidden') === 'true') return false;
var _getComputedStyle = getComputedStyle(node),
display = _getComputedStyle.display,
visibility = _getComputedStyle.visibility;
if (display === 'none' || visibility === 'hidden') return false;
node = node.parentElement;
}
return true;
};
// enclosing <ul> count
var getMenuItemDepth = function getMenuItemDepth(el, scope) {
var depth = 0;
var node = el.parentElement;
while (node && scope.contains(node)) {
if (node.tagName === 'UL') depth++;
node = node.parentElement;
}
return depth;
};
var getReachableMenuItems = function getReachableMenuItems(scope) {
return Array.from(scope.querySelectorAll(MENU_ITEM_SELECTOR)).filter(function (el) {
return !isMenuItemDisabled(el) && isMenuItemReachable(el, scope);
});
};
// focus without page scroll
var focusMenuItem = function focusMenuItem(scope, el) {
focus(el, {
preventScroll: true
});
scrollMenuItemIntoView(el, scope);
};
// keep the row visible inside the sidebar scroller (never the window)
var scrollMenuItemIntoView = function scrollMenuItemIntoView(el, scope) {
var container = el.parentElement;
while (container && container !== scope) {
var style = getComputedStyle(container);
var scrolls = /(auto|scroll|overlay)/.test(style.overflowY) && container.scrollHeight > container.clientHeight;
if (scrolls) {
var item = el.getBoundingClientRect();
var view = container.getBoundingClientRect();
if (item.top < view.top) container.scrollTop -= view.top - item.top;else if (item.bottom > view.bottom) container.scrollTop += item.bottom - view.bottom;
return;
}
container = container.parentElement;
}
};
var onSidebarMenuKeyDown = function onSidebarMenuKeyDown(event) {
var scope = event.currentTarget;
var target = event.target;
if (!scope || !(target !== null && target !== void 0 && target.closest)) return;
var current = target.closest(MENU_ITEM_SELECTOR);
// skip when off a menu item, or on a popup trigger (it owns its keys)
if (!current || !scope.contains(current) || current.hasAttribute('aria-haspopup')) return;
var items = getReachableMenuItems(scope);
var index = items.indexOf(current);
if (index === -1) return;
var move = function move(to) {
if (!to || to === current) return;
event.preventDefault();
focusMenuItem(scope, to);
};
switch (event.code) {
case 'ArrowDown':
move(items[index + 1]);
break;
case 'ArrowUp':
move(items[index - 1]);
break;
case 'Home':
move(items[0]);
break;
case 'End':
move(items[items.length - 1]);
break;
case 'ArrowRight':
{
if (!isMenuItemBranch(current)) break;
if (!isMenuItemExpanded(current)) {
event.preventDefault();
current.click(); // expand
} else {
var child = items[index + 1];
// into first child (real descendant only)
if (child && getMenuItemDepth(child, scope) > getMenuItemDepth(current, scope)) move(child);
}
break;
}
case 'ArrowLeft':
{
if (isMenuItemBranch(current) && isMenuItemExpanded(current)) {
event.preventDefault();
current.click(); // collapse
break;
}
// to parent (nearest preceding row one level up)
var parentDepth = getMenuItemDepth(current, scope) - 1;
for (var i = index - 1; parentDepth >= 1 && i >= 0; i--) {
if (getMenuItemDepth(items[i], scope) === parentDepth) {
move(items[i]);
break;
}
}
break;
}
}
};
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var script = {
name: 'Sidebar',
"extends": script$1,
inheritAttrs: false,
emits: ['update:open'],
inject: {
$pcSidebarLayout: {
"default": null
}
},
data: function data() {
return {
d_open: this.open !== undefined ? this.open : true
};
},
watch: {
open: function open(newValue) {
if (newValue === undefined) return;
this.d_open = newValue;
},
id: function id(newValue, oldValue) {
if (oldValue) {
this.unregisterFromLayout(oldValue);
}
if (newValue) {
this.registerInLayout(newValue);
}
}
},
mounted: function mounted() {
this.registerInLayout(this.id);
},
beforeUnmount: function beforeUnmount() {
this.clearHoverTimer();
this.unregisterFromLayout(this.id);
},
methods: {
registerInLayout: function registerInLayout(id) {
if (!this.$pcSidebarLayout || !id) return;
var self = this;
this.$pcSidebarLayout.registerSidebar(id, {
toggle: function toggle() {
return self.toggle();
},
get open() {
return self.d_open;
},
get state() {
return self.displayState;
},
get dismissable() {
return self.dismissable;
},
get overlay() {
return self.overlay;
},
get hideOnOutsideClick() {
return self.hideOnOutsideClick;
}
});
},
unregisterFromLayout: function unregisterFromLayout(id) {
if (this.$pcSidebarLayout && id) {
this.$pcSidebarLayout.unregisterSidebar(id);
}
},
clearHoverTimer: function clearHoverTimer() {
if (this.hoverTimer) {
clearTimeout(this.hoverTimer);
this.hoverTimer = null;
}
},
updateOpen: function updateOpen(value) {
var next = !!value;
if (this.open === undefined) {
this.d_open = next;
}
this.$emit('update:open', next);
},
toggle: function toggle() {
if (this.collapsible === 'none') return;
this.updateOpen(!this.d_open);
},
expand: function expand() {
this.updateOpen(true);
},
collapse: function collapse() {
if (this.collapsible === 'none') return;
this.updateOpen(false);
},
onPointerEnter: function onPointerEnter() {
var _this = this;
if (!this.openOnHover) return;
this.clearHoverTimer();
this.hoverTimer = setTimeout(function () {
return _this.expand();
}, this.hoverOpenDelay);
},
onPointerLeave: function onPointerLeave() {
var _this2 = this;
if (!this.openOnHover) return;
this.clearHoverTimer();
this.hoverTimer = setTimeout(function () {
return _this2.collapse();
}, this.hoverCloseDelay);
},
onKeyDown: function onKeyDown(event) {
onSidebarMenuKeyDown(event);
},
getSectionAttrs: function getSectionAttrs(section) {
return {
'data-pc-name': 'sidebar',
'data-pc-section': section,
'data-state': this.displayState,
'data-collapsible': this.displayState === 'collapsed' ? this.collapsible : undefined,
'data-collapsible-mode': this.collapsible,
'data-variant': this.variant,
'data-side': this.side,
'data-overlay': this.overlay ? '' : undefined
};
}
},
computed: {
displayState: function displayState() {
return this.d_open ? 'expanded' : 'collapsed';
},
a11yAttrs: function a11yAttrs() {
var hover = this.openOnHover ? {
onPointerenter: this.onPointerEnter,
onPointerleave: this.onPointerLeave
} : {};
return _objectSpread(_objectSpread({
id: this.id
}, this.getSectionAttrs('root')), {}, {
onKeydown: this.onKeyDown,
style: {
'--px-sidebar-width': this.width,
'--px-sidebar-width-icon': this.iconWidth
}
}, hover);
},
attrs: function attrs() {
return mergeProps(this.a11yAttrs, this.ptmi('root'));
}
}
};
function render(_ctx, _cache, $props, $setup, $data, $options) {
return !_ctx.asChild ? (openBlock(), createBlock(resolveDynamicComponent(_ctx.as), mergeProps({
key: 0,
"class": _ctx.cx('root')
}, $options.attrs), {
"default": withCtx(function () {
return [renderSlot(_ctx.$slots, "default")];
}),
_: 3
}, 16, ["class"])) : renderSlot(_ctx.$slots, "default", {
a11yAttrs: $options.a11yAttrs,
"class": normalizeClass(_ctx.cx('root'))
}, undefined, undefined, 1);
}
script.render = render;
export { script as default };