@stories-js/core
Version:
Stories web components to build stories
384 lines • 11.5 kB
JavaScript
/*!
* (C) StoriesJS https://storiesjs.org - GPL-2.0 License
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Component, Host, h, Element, Prop, Event } from '@stencil/core';
import { hasShadowDom, inheritAttributes } from '../../helpers';
import { createColorClasses, hostContext } from '../../utils';
export class Button {
constructor() {
this.inItem = false;
this.inListHeader = false;
this.inToolbar = false;
this.inheritedAttributes = {};
/**
* The type of button.
*/
this.buttonType = 'button';
/**
* If `true`, the user cannot interact with the button.
*/
this.disabled = false;
/**
* When using a router, it specifies the transition direction when navigating to
* another page using `href`.
*/
this.routerDirection = 'forward';
/**
* The type of the button.
*/
this.type = 'button';
/**
* If `true`, activates a button with a heavier font weight.
*/
this.strong = false;
this.handleClick = (ev) => {
if (this.type === 'button') {
this.storiesClick.emit();
}
else if (hasShadowDom(this.el)) {
// this button wants to specifically submit a form
// climb up the dom to see if we're in a <form>
// and if so, then use JS to submit/reset it
const form = this.el.closest('form');
if (form) {
ev.preventDefault();
const fakeButton = document.createElement('button');
fakeButton.type = this.type;
fakeButton.style.display = 'none';
form.appendChild(fakeButton);
fakeButton.click();
fakeButton.remove();
}
}
};
this.onFocus = () => {
this.storiesFocus.emit();
};
this.onBlur = () => {
this.storiesBlur.emit();
};
}
componentWillLoad() {
this.inToolbar = !!this.el.closest('stories-buttons');
this.inListHeader = !!this.el.closest('stories-list-header');
this.inItem = !!this.el.closest('stories-item') || !!this.el.closest('stories-item-divider');
this.inheritedAttributes = inheritAttributes(this.el, ['aria-label']);
}
get hasIconOnly() {
return !!this.el.querySelector('[slot="icon-only"]');
}
render() {
const { buttonType, type, disabled, target, size, href, color, expand, hasIconOnly, shape, strong, inheritedAttributes } = this;
const finalSize = size === undefined && this.inItem ? 'small' : size;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const TagType = href === undefined ? 'button' : 'a';
const attrs = (TagType === 'button')
? { type }
: {
href,
target
};
let fill = this.fill;
if (fill === undefined) {
fill = this.inToolbar || this.inListHeader ? 'clear' : 'solid';
}
return (h(Host, { "aria-disabled": disabled ? 'true' : null, class: createColorClasses(color, {
[buttonType]: true,
[`${buttonType}-${expand}`]: expand !== undefined,
[`${buttonType}-${finalSize}`]: finalSize !== undefined,
[`${buttonType}-${shape}`]: shape !== undefined,
[`${buttonType}-${fill}`]: true,
[`${buttonType}-strong`]: strong,
'in-toolbar': hostContext('stories-toolbar', this.el),
'in-toolbar-color': hostContext('stories-toolbar[color]', this.el),
'button-has-icon-only': hasIconOnly,
'button-disabled': disabled,
'stories-activatable': true,
'stories-focusable': true,
}), onClick: this.handleClick },
h(TagType, Object.assign({}, attrs, { class: "button-native", disabled: disabled, onBlur: this.onBlur, onFocus: this.onFocus, part: "native" }, inheritedAttributes),
h("span", { class: "button-inner" },
h("slot", { name: "icon-only" }),
h("slot", { name: "start" }),
h("slot", null),
h("slot", { name: "end" })))));
}
static get is() { return "stories-button"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() { return {
"$": ["button.scss"]
}; }
static get styleUrls() { return {
"$": ["button.css"]
}; }
static get properties() { return {
"color": {
"type": "string",
"mutable": false,
"complexType": {
"original": "Color",
"resolved": "string",
"references": {
"Color": {
"location": "import",
"path": "../../types"
}
}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The color to use from your application's color palette.\nDefault options are: `\"primary\"`, `\"secondary\"`, `\"tertiary\"`, `\"success\"`, `\"warning\"`, `\"danger\"`, `\"light\"`, `\"medium\"`, and `\"dark\"`.\nFor more information on colors, see [theming](/docs/theming/basics)."
},
"attribute": "color",
"reflect": true
},
"buttonType": {
"type": "string",
"mutable": true,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "The type of button."
},
"attribute": "button-type",
"reflect": false,
"defaultValue": "'button'"
},
"disabled": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "If `true`, the user cannot interact with the button."
},
"attribute": "disabled",
"reflect": true,
"defaultValue": "false"
},
"expand": {
"type": "string",
"mutable": false,
"complexType": {
"original": "'full' | 'block'",
"resolved": "\"block\" | \"full\"",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Set to `\"block\"` for a full-width button or to `\"full\"` for a full-width button\nwithout left and right borders."
},
"attribute": "expand",
"reflect": true
},
"fill": {
"type": "string",
"mutable": true,
"complexType": {
"original": "'clear' | 'outline' | 'solid' | 'default'",
"resolved": "\"clear\" | \"default\" | \"outline\" | \"solid\"",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "Set to `\"clear\"` for a transparent button, to `\"outline\"` for a transparent\nbutton with a border, or to `\"solid\"`. The default style is `\"solid\"` except inside of\na toolbar, where the default is `\"clear\"`."
},
"attribute": "fill",
"reflect": true
},
"routerDirection": {
"type": "string",
"mutable": false,
"complexType": {
"original": "RouterDirection",
"resolved": "\"back\" | \"forward\" | \"root\"",
"references": {
"RouterDirection": {
"location": "import",
"path": "../../types"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When using a router, it specifies the transition direction when navigating to\nanother page using `href`."
},
"attribute": "router-direction",
"reflect": false,
"defaultValue": "'forward'"
},
"href": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string | undefined",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Contains a URL or a URL fragment that the hyperlink points to.\nIf this property is set, an anchor tag will be rendered."
},
"attribute": "href",
"reflect": false
},
"shape": {
"type": "string",
"mutable": false,
"complexType": {
"original": "'round'",
"resolved": "\"round\"",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The button shape."
},
"attribute": "shape",
"reflect": true
},
"type": {
"type": "string",
"mutable": false,
"complexType": {
"original": "'submit' | 'reset' | 'button'",
"resolved": "\"button\" | \"reset\" | \"submit\"",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "The type of the button."
},
"attribute": "type",
"reflect": false,
"defaultValue": "'button'"
},
"size": {
"type": "string",
"mutable": false,
"complexType": {
"original": "'small' | 'default' | 'large'",
"resolved": "\"default\" | \"large\" | \"small\"",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "The button size."
},
"attribute": "size",
"reflect": true
},
"strong": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "If `true`, activates a button with a heavier font weight."
},
"attribute": "strong",
"reflect": false,
"defaultValue": "false"
},
"target": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string | undefined",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies where to display the linked URL.\nOnly applies when an `href` is provided.\nSpecial keywords: `\"_blank\"`, `\"_self\"`, `\"_parent\"`, `\"_top\"`."
},
"attribute": "target",
"reflect": false
}
}; }
static get events() { return [{
"method": "storiesFocus",
"name": "storiesFocus",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Emitted when the button has focus."
},
"complexType": {
"original": "void",
"resolved": "void",
"references": {}
}
}, {
"method": "storiesBlur",
"name": "storiesBlur",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Emitted when the button loses focus."
},
"complexType": {
"original": "void",
"resolved": "void",
"references": {}
}
}, {
"method": "storiesClick",
"name": "storiesClick",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Emitted when the button click."
},
"complexType": {
"original": "void",
"resolved": "void",
"references": {}
}
}]; }
static get elementRef() { return "el"; }
}
//# sourceMappingURL=button.js.map