@ionic/core
Version:
Base components for Ionic
147 lines (146 loc) • 4.94 kB
JavaScript
import { hasShadowDom } from '../../utils/helpers';
import { createColorClasses, openURL } from '../../utils/theme';
export class Button {
constructor() {
this.inToolbar = false;
this.buttonType = 'button';
this.disabled = false;
this.routerDirection = 'forward';
this.strong = false;
this.type = 'button';
this.onFocus = () => {
this.ionFocus.emit();
};
this.onBlur = () => {
this.ionBlur.emit();
};
}
componentWillLoad() {
this.inToolbar = !!this.el.closest('ion-buttons');
}
onClick(ev) {
if (this.type === 'button') {
openURL(this.win, this.href, ev, this.routerDirection);
}
else if (hasShadowDom(this.el)) {
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();
}
}
}
hostData() {
const { buttonType, disabled, color, expand, shape, size, strong } = this;
let fill = this.fill;
if (fill === undefined) {
fill = this.inToolbar ? 'clear' : 'solid';
}
return {
'aria-disabled': disabled ? 'true' : null,
class: Object.assign({}, createColorClasses(color), { [buttonType]: true, [`${buttonType}-${expand}`]: expand !== undefined, [`${buttonType}-${size}`]: size !== undefined, [`${buttonType}-${shape}`]: shape !== undefined, [`${buttonType}-${fill}`]: true, [`${buttonType}-strong`]: strong, 'button-disabled': disabled, 'ion-activatable': true, 'ion-focusable': true })
};
}
render() {
const TagType = this.href === undefined ? 'button' : 'a';
const attrs = (TagType === 'button')
? { type: this.type }
: { href: this.href };
return (h(TagType, Object.assign({}, attrs, { class: "button-native", disabled: this.disabled, onFocus: this.onFocus, onBlur: this.onBlur }),
h("span", { class: "button-inner" },
h("slot", { name: "icon-only" }),
h("slot", { name: "start" }),
h("slot", null),
h("slot", { name: "end" })),
this.mode === 'md' && h("ion-ripple-effect", { type: this.inToolbar ? 'unbounded' : 'bounded' })));
}
static get is() { return "ion-button"; }
static get encapsulation() { return "shadow"; }
static get properties() { return {
"buttonType": {
"type": String,
"attr": "button-type",
"mutable": true
},
"color": {
"type": String,
"attr": "color"
},
"disabled": {
"type": Boolean,
"attr": "disabled",
"reflectToAttr": true
},
"el": {
"elementRef": true
},
"expand": {
"type": String,
"attr": "expand",
"reflectToAttr": true
},
"fill": {
"type": String,
"attr": "fill",
"reflectToAttr": true,
"mutable": true
},
"href": {
"type": String,
"attr": "href"
},
"mode": {
"type": String,
"attr": "mode"
},
"routerDirection": {
"type": String,
"attr": "router-direction"
},
"shape": {
"type": String,
"attr": "shape",
"reflectToAttr": true
},
"size": {
"type": String,
"attr": "size",
"reflectToAttr": true
},
"strong": {
"type": Boolean,
"attr": "strong"
},
"type": {
"type": String,
"attr": "type"
},
"win": {
"context": "window"
}
}; }
static get events() { return [{
"name": "ionFocus",
"method": "ionFocus",
"bubbles": true,
"cancelable": true,
"composed": true
}, {
"name": "ionBlur",
"method": "ionBlur",
"bubbles": true,
"cancelable": true,
"composed": true
}]; }
static get listeners() { return [{
"name": "click",
"method": "onClick"
}]; }
static get style() { return "/**style-placeholder:ion-button:**/"; }
static get styleMode() { return "/**style-id-placeholder:ion-button:**/"; }
}