igniteui-webcomponents
Version:
Ignite UI for Web Components is a complete library of UI components, giving you the ability to build modern web applications using encapsulation and the concept of reusable components in a dependency-free approach.
213 lines • 7.07 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { html, LitElement } from 'lit';
import { property } from 'lit/decorators.js';
import { cache } from 'lit/directives/cache.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { createRef, ref } from 'lit/directives/ref.js';
import { addThemingController } from '../../theming/theming-controller.js';
import { addCommandController } from '../common/controllers/command.js';
import { addSlotController, setSlots } from '../common/controllers/slot.js';
import { registerComponent } from '../common/definitions/register.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { partMap } from '../common/part-map.js';
import { bindIf, isPopoverOpen, numberInRangeInclusive, } from '../common/util.js';
import IgcNavDrawerHeaderItemComponent from './nav-drawer-header-item.js';
import IgcNavDrawerItemComponent from './nav-drawer-item.js';
import { styles } from './themes/container.base.css.js';
import { all } from './themes/container.js';
import { styles as shared } from './themes/shared/container/nav-drawer.common.css.js';
export default class IgcNavDrawerComponent extends EventEmitterMixin(LitElement) {
static { this.tagName = 'igc-nav-drawer'; }
static { this.styles = [styles, shared]; }
static register() {
registerComponent(IgcNavDrawerComponent, IgcNavDrawerHeaderItemComponent, IgcNavDrawerItemComponent);
}
get _dialog() {
return this._dialogRef.value;
}
get _mini() {
return this._miniRef.value;
}
get _hasMiniContent() {
return this._slots.hasAssignedElements('mini');
}
get _isRelative() {
return this.position === 'relative';
}
constructor() {
super();
this._dialogRef = createRef();
this._miniRef = createRef();
this._slots = addSlotController(this, {
slots: setSlots('mini'),
onChange: this._handleMiniState,
});
this.position = 'start';
this.open = false;
this.keepOpenOnEscape = false;
addThemingController(this, all);
addCommandController(this)
.set('--show', this.show)
.set('--hide', this.hide)
.set('--toggle', this.toggle);
}
update(properties) {
if (properties.has('position') && this._isRelative) {
this._dialog?.close();
if (isPopoverOpen(this._mini)) {
this._mini?.hidePopover();
}
}
super.update(properties);
}
updated(properties) {
if (properties.has('open') || properties.has('position')) {
this._handleOpenState();
this._handleMiniState();
}
}
_handleOpenState() {
if (this._isRelative) {
return;
}
this.open ? this._dialog?.showModal() : this._dialog?.close();
}
_handleMiniState() {
if (this._isRelative) {
return;
}
const mini = this._mini;
if (!mini) {
return;
}
const popOverOpen = isPopoverOpen(mini);
if (!this._hasMiniContent || this.open) {
if (popOverOpen) {
mini.hidePopover();
}
}
else if (!popOverOpen) {
mini.showPopover();
}
}
_handleCancel(event) {
event.preventDefault();
if (!this.keepOpenOnEscape) {
this._closeWithEvent();
}
}
_handleClose() {
if (this.open) {
this._dialog?.showModal();
}
}
_handleClick({ clientX, clientY, target }) {
if (this._dialog === target) {
const rect = this._dialog.getBoundingClientRect();
const inX = numberInRangeInclusive(clientX, rect.left, rect.right);
const inY = numberInRangeInclusive(clientY, rect.top, rect.bottom);
if (!(inX && inY)) {
this._closeWithEvent();
}
}
}
_emitClosing() {
return this.emitEvent('igcClosing', { cancelable: true });
}
async _closeWithEvent() {
if (!(this.open && this._emitClosing())) {
return false;
}
this.open = false;
await this.updateComplete;
this.emitEvent('igcClosed');
return true;
}
async show() {
if (this.open) {
return false;
}
this.open = true;
await this.updateComplete;
return true;
}
async hide() {
if (!this.open) {
return false;
}
this.open = false;
await this.updateComplete;
return true;
}
toggle() {
return this.open ? this.hide() : this.show();
}
_renderMiniVariant() {
return html `
<nav
${ref(this._miniRef)}
aria-label=${ifDefined(this.label)}
part=${partMap({
mini: true,
hidden: !this._hasMiniContent,
})}
.inert=${this.open || !this._hasMiniContent}
.popover=${!this._isRelative ? 'manual' : null}
>
<slot name="mini"></slot>
</nav>
`;
}
_renderContent() {
return html `
<div part="main">
<slot></slot>
</div>
`;
}
_renderDialog() {
return html `
<dialog
${ref(this._dialogRef)}
part="base"
aria-modal="true"
aria-label=${ifDefined(this.label)}
=${this._handleClick}
=${this._handleCancel}
=${bindIf(this.keepOpenOnEscape, this._handleClose)}
>
${this._renderContent()}
</dialog>
${this._renderMiniVariant()}
`;
}
_renderRelative() {
return html `
<nav part="base" aria-label=${ifDefined(this.label)} .inert=${!this.open}>
${this._renderContent()}
</nav>
${this._renderMiniVariant()}
`;
}
render() {
return html `${cache(this._isRelative ? this._renderRelative() : this._renderDialog())}`;
}
}
__decorate([
property({ reflect: true })
], IgcNavDrawerComponent.prototype, "position", void 0);
__decorate([
property({ type: Boolean, reflect: true })
], IgcNavDrawerComponent.prototype, "open", void 0);
__decorate([
property({ type: Boolean, attribute: 'keep-open-on-escape' })
], IgcNavDrawerComponent.prototype, "keepOpenOnEscape", void 0);
__decorate([
property()
], IgcNavDrawerComponent.prototype, "label", void 0);
//# sourceMappingURL=nav-drawer.js.map