@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
390 lines (389 loc) • 12.2 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
* v1.5.0-next.4
*/
import { h } from "@stencil/core";
import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot";
import { getSlotted, setRequestedIcon } from "../../utils/dom";
import { componentLoaded, setComponentLoaded, setUpLoadableComponent } from "../../utils/loadable";
import { connectLocalized, disconnectLocalized } from "../../utils/locale";
import { connectMessages, disconnectMessages, setUpMessages, updateMessages } from "../../utils/t9n";
import { KindIcons } from "../resources";
import { CSS, SLOTS } from "./resources";
/**
* Notices are intended to be used to present users with important-but-not-crucial contextual tips or copy. Because
* notices are displayed inline, a common use case is displaying them on page-load to present users with short hints or contextual copy.
* They are optionally closable - useful for keeping track of whether or not a user has closed the notice. You can also choose not
* to display a notice on page load and set the "active" attribute as needed to contextually provide inline messaging to users.
*/
/**
* @slot title - A slot for adding the title.
* @slot message - A slot for adding the message.
* @slot link - A slot for adding a `calcite-action` to take, such as: "undo", "try again", "link to page", etc.
* @slot actions-end - A slot for adding `calcite-action`s to the end of the component. It is recommended to use two or less actions.
*/
export class Notice {
constructor() {
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
this.close = () => {
this.open = false;
this.calciteNoticeClose.emit();
};
this.open = false;
this.kind = "brand";
this.closable = false;
this.icon = undefined;
this.iconFlipRtl = false;
this.scale = "m";
this.width = "auto";
this.messages = undefined;
this.messageOverrides = undefined;
this.effectiveLocale = undefined;
this.defaultMessages = undefined;
}
onMessagesChange() {
/* wired up by t9n util */
}
updateRequestedIcon() {
this.requestedIcon = setRequestedIcon(KindIcons, this.icon, this.kind);
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
connectConditionalSlotComponent(this);
connectLocalized(this);
connectMessages(this);
}
disconnectedCallback() {
disconnectConditionalSlotComponent(this);
disconnectLocalized(this);
disconnectMessages(this);
}
async componentWillLoad() {
setUpLoadableComponent(this);
this.requestedIcon = setRequestedIcon(KindIcons, this.icon, this.kind);
await setUpMessages(this);
}
componentDidLoad() {
setComponentLoaded(this);
}
render() {
const { el } = this;
const closeButton = (h("button", { "aria-label": this.messages.close, class: CSS.close, onClick: this.close,
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => (this.closeButton = el) }, h("calcite-icon", { icon: "x", scale: this.scale === "l" ? "m" : "s" })));
const hasActionEnd = getSlotted(el, SLOTS.actionsEnd);
return (h("div", { class: CSS.container }, this.requestedIcon ? (h("div", { class: CSS.icon }, h("calcite-icon", { flipRtl: this.iconFlipRtl, icon: this.requestedIcon, scale: this.scale === "l" ? "m" : "s" }))) : null, h("div", { class: CSS.content }, h("slot", { name: SLOTS.title }), h("slot", { name: SLOTS.message }), h("slot", { name: SLOTS.link })), hasActionEnd ? (h("div", { class: CSS.actionsEnd }, h("slot", { name: SLOTS.actionsEnd }))) : null, this.closable ? closeButton : null));
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component's first focusable element. */
async setFocus() {
await componentLoaded(this);
const noticeLinkEl = this.el.querySelector("calcite-link");
if (!this.closeButton && !noticeLinkEl) {
return;
}
if (noticeLinkEl) {
noticeLinkEl.setFocus();
}
else if (this.closeButton) {
this.closeButton.focus();
}
}
effectiveLocaleChange() {
updateMessages(this, this.effectiveLocale);
}
static get is() { return "calcite-notice"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["notice.scss"]
};
}
static get styleUrls() {
return {
"$": ["notice.css"]
};
}
static get assetsDirs() { return ["assets"]; }
static get properties() {
return {
"open": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, the component is visible."
},
"attribute": "open",
"reflect": true,
"defaultValue": "false"
},
"kind": {
"type": "string",
"mutable": false,
"complexType": {
"original": "Extract<\n \"brand\" | \"danger\" | \"info\" | \"success\" | \"warning\",\n Kind\n >",
"resolved": "\"brand\" | \"danger\" | \"info\" | \"success\" | \"warning\"",
"references": {
"Extract": {
"location": "global"
},
"Kind": {
"location": "import",
"path": "../interfaces"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies the kind of the component (will apply to top border and icon)."
},
"attribute": "kind",
"reflect": true,
"defaultValue": "\"brand\""
},
"closable": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, a close button is added to the component."
},
"attribute": "closable",
"reflect": true,
"defaultValue": "false"
},
"icon": {
"type": "any",
"mutable": false,
"complexType": {
"original": "string | boolean",
"resolved": "boolean | string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, shows a default recommended icon. Alternatively, pass a Calcite UI Icon name to display a specific icon."
},
"attribute": "icon",
"reflect": true
},
"iconFlipRtl": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, the icon will be flipped when the element direction is right-to-left (`\"rtl\"`)."
},
"attribute": "icon-flip-rtl",
"reflect": true,
"defaultValue": "false"
},
"scale": {
"type": "string",
"mutable": false,
"complexType": {
"original": "Scale",
"resolved": "\"l\" | \"m\" | \"s\"",
"references": {
"Scale": {
"location": "import",
"path": "../interfaces"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies the size of the component."
},
"attribute": "scale",
"reflect": true,
"defaultValue": "\"m\""
},
"width": {
"type": "string",
"mutable": false,
"complexType": {
"original": "Width",
"resolved": "\"auto\" | \"full\" | \"half\"",
"references": {
"Width": {
"location": "import",
"path": "../interfaces"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies the width of the component."
},
"attribute": "width",
"reflect": true,
"defaultValue": "\"auto\""
},
"messages": {
"type": "unknown",
"mutable": true,
"complexType": {
"original": "NoticeMessages",
"resolved": "{ close: string; }",
"references": {
"NoticeMessages": {
"location": "import",
"path": "./assets/notice/t9n"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": "Made into a prop for testing purposes only"
}
},
"messageOverrides": {
"type": "unknown",
"mutable": true,
"complexType": {
"original": "Partial<NoticeMessages>",
"resolved": "{ close?: string; }",
"references": {
"Partial": {
"location": "global"
},
"NoticeMessages": {
"location": "import",
"path": "./assets/notice/t9n"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Use this property to override individual strings used by the component."
}
}
};
}
static get states() {
return {
"effectiveLocale": {},
"defaultMessages": {}
};
}
static get events() {
return [{
"method": "calciteNoticeClose",
"name": "calciteNoticeClose",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [],
"text": "Fired when the component is closed."
},
"complexType": {
"original": "void",
"resolved": "void",
"references": {}
}
}, {
"method": "calciteNoticeOpen",
"name": "calciteNoticeOpen",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [],
"text": "Fired when the component is opened."
},
"complexType": {
"original": "void",
"resolved": "void",
"references": {}
}
}];
}
static get methods() {
return {
"setFocus": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Sets focus on the component's first focusable element.",
"tags": []
}
}
};
}
static get elementRef() { return "el"; }
static get watchers() {
return [{
"propName": "messageOverrides",
"methodName": "onMessagesChange"
}, {
"propName": "icon",
"methodName": "updateRequestedIcon"
}, {
"propName": "kind",
"methodName": "updateRequestedIcon"
}, {
"propName": "effectiveLocale",
"methodName": "effectiveLocaleChange"
}];
}
}