@material/icon-toggle
Version:
The Material Components for the web icon toggle component
121 lines (104 loc) • 3.98 kB
JavaScript
/**
* @license
* Copyright 2016 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import MDCComponent from '@material/base/component';
import MDCIconToggleFoundation from './foundation';
import {MDCRipple, MDCRippleFoundation} from '@material/ripple/index';
/**
* @extends {MDCComponent<!MDCIconToggleFoundation>}
*/
class MDCIconToggle extends MDCComponent {
static attachTo(root) {
return new MDCIconToggle(root);
}
constructor(...args) {
super(...args);
/** @private {!MDCRipple} */
this.ripple_ = this.initRipple_();
}
/** @return {!Element} */
get iconEl_() {
const {'iconInnerSelector': sel} = this.root_.dataset;
return sel ?
/** @type {!Element} */ (this.root_.querySelector(sel)) : this.root_;
}
/**
* @return {!MDCRipple}
* @private
*/
initRipple_() {
const adapter = Object.assign(MDCRipple.createAdapter(this), {
isUnbounded: () => true,
isSurfaceActive: () => this.foundation_.isKeyboardActivated(),
});
const foundation = new MDCRippleFoundation(adapter);
return new MDCRipple(this.root_, foundation);
}
destroy() {
this.ripple_.destroy();
super.destroy();
}
/** @return {!MDCIconToggleFoundation} */
getDefaultFoundation() {
return new MDCIconToggleFoundation({
addClass: (className) => this.iconEl_.classList.add(className),
removeClass: (className) => this.iconEl_.classList.remove(className),
registerInteractionHandler: (type, handler) => this.root_.addEventListener(type, handler),
deregisterInteractionHandler: (type, handler) => this.root_.removeEventListener(type, handler),
setText: (text) => this.iconEl_.textContent = text,
getTabIndex: () => /* number */ this.root_.tabIndex,
setTabIndex: (tabIndex) => this.root_.tabIndex = tabIndex,
getAttr: (name, value) => this.root_.getAttribute(name, value),
setAttr: (name, value) => this.root_.setAttribute(name, value),
rmAttr: (name) => this.root_.removeAttribute(name),
notifyChange: (evtData) => this.emit(MDCIconToggleFoundation.strings.CHANGE_EVENT, evtData),
});
}
initialSyncWithDOM() {
this.on = this.root_.getAttribute(MDCIconToggleFoundation.strings.ARIA_PRESSED) === 'true';
this.disabled = this.root_.getAttribute(MDCIconToggleFoundation.strings.ARIA_DISABLED) === 'true';
}
/** @return {!MDCRipple} */
get ripple() {
return this.ripple_;
}
/** @return {boolean} */
get on() {
return this.foundation_.isOn();
}
/** @param {boolean} isOn */
set on(isOn) {
this.foundation_.toggle(isOn);
}
/** @return {boolean} */
get disabled() {
return this.foundation_.isDisabled();
}
/** @param {boolean} isDisabled */
set disabled(isDisabled) {
this.foundation_.setDisabled(isDisabled);
}
refreshToggleData() {
this.foundation_.refreshToggleData();
}
}
export {MDCIconToggle, MDCIconToggleFoundation};