preact-material-components
Version:
preact wrapper for "Material Components for the web"
220 lines (176 loc) • 5.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _preact = require("preact");
var _MaterialComponent = _interopRequireDefault(require("../MaterialComponent"));
var _temporary = require("@material/drawer/temporary");
var _persistent = require("@material/drawer/persistent");
var _List = _interopRequireDefault(require("../List"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
/**
* Default props for drawers
*/
const defaultProps = {
open: false
};
class TemporaryDrawer extends _MaterialComponent.default {
constructor() {
super();
this.componentName = 'drawer--temporary';
this._open = this._open.bind(this);
this._close = this._close.bind(this);
}
_open(e) {
if (this.props.onOpen) {
this.props.onOpen(e);
}
}
_close(e) {
if (this.props.onClose) {
this.props.onClose(e);
}
}
componentDidMount() {
this.MDComponent = _temporary.MDCTemporaryDrawer.attachTo(this.control);
this.MDComponent.listen('MDCTemporaryDrawer:open', this._open);
this.MDComponent.listen('MDCTemporaryDrawer:close', this._close);
toggleDrawer(defaultProps, this.props, this.MDComponent);
}
componentWillUnmount() {
this.MDComponent.unlisten('MDCTemporaryDrawer:close', this._close);
this.MDComponent.unlisten('MDCTemporaryDrawer:open', this._open);
this.MDComponent.destroy && this.MDComponent.destroy();
}
componentWillUpdate(nextProps) {
toggleDrawer(this.props, nextProps, this.MDComponent);
}
materialDom(props) {
return (0, _preact.h)("aside", _extends({
className: "mdc-typography mdc-drawer",
ref: this.setControlRef
}, props), (0, _preact.h)("nav", {
className: "mdc-drawer__drawer"
}, props.children));
}
}
/**
* @prop spacer = false
*/
class PermanentDrawer extends _MaterialComponent.default {
constructor() {
super();
this.componentName = 'drawer--permanent';
}
materialDom(props) {
return (0, _preact.h)("nav", _extends({
className: "mdc-typography mdc-drawer"
}, props), props.spacer && (0, _preact.h)("div", {
className: "mdc-drawer__toolbar-spacer"
}), (0, _preact.h)("div", {
className: "mdc-drawer__content"
}, (0, _preact.h)("nav", {
className: "mdc-list"
}, props.children)));
}
}
class PersistentDrawer extends _MaterialComponent.default {
constructor() {
super();
this.componentName = 'drawer--persistent';
this._open = this._open.bind(this);
this._close = this._close.bind(this);
}
_open(e) {
if (this.props.onOpen) {
this.props.onOpen(e);
}
}
_close(e) {
if (this.props.onClose) {
this.props.onClose(e);
}
}
componentDidMount() {
this.MDComponent = _persistent.MDCPersistentDrawer.attachTo(this.control);
this.MDComponent.listen('MDCPersistentDrawer:open', this._open);
this.MDComponent.listen('MDCPersistentDrawer:close', this._close);
toggleDrawer(defaultProps, this.props, this.MDComponent);
}
componentWillUnmount() {
this.MDComponent.unlisten('MDCPersistentDrawer:close', this._close);
this.MDComponent.unlisten('MDCPersistentDrawer:open', this._open);
this.MDComponent.destroy && this.MDComponent.destroy();
}
componentWillUpdate(nextProps) {
toggleDrawer(this.props, nextProps, this.MDComponent);
}
materialDom(props) {
return (0, _preact.h)("aside", _extends({
className: "mdc-typography mdc-drawer",
ref: this.setControlRef
}, props), (0, _preact.h)("nav", {
className: "mdc-drawer__drawer"
}, props.children));
}
}
class DrawerHeader extends _MaterialComponent.default {
constructor() {
super();
this.componentName = 'drawer__header';
}
materialDom(props) {
return (0, _preact.h)("header", _extends({
ref: this.setControlRef
}, props), (0, _preact.h)("div", {
className: "mdc-drawer__header-content"
}, props.children));
}
}
class DrawerContent extends _MaterialComponent.default {
constructor() {
super();
this.componentName = 'drawer__content';
}
materialDom(props) {
return (0, _preact.h)("nav", _extends({
className: "mdc-list",
ref: this.setControlRef
}, props), props.children);
}
}
/**
* @prop selected = false
*/
class DrawerItem extends _List.default.LinkItem {
constructor() {
super();
}
materialDom(props) {
const returnedNode = super.materialDom(props);
/* Logic to add selected class */
if (props.selected) {
returnedNode.attributes['className'] = 'mdc-list-item--activated';
}
return returnedNode;
}
}
/**
* Function to add declarative opening/closing to drawer
*/
function toggleDrawer(oldprops, newprops, drawer) {
if ('open' in oldprops && 'open' in newprops && oldprops.open !== newprops.open) {
drawer.open = newprops.open;
}
}
let Drawer = {};
Drawer.DrawerItem = DrawerItem;
Drawer.TemporaryDrawer = TemporaryDrawer;
Drawer.DrawerHeader = DrawerHeader;
Drawer.DrawerContent = DrawerContent;
Drawer.PermanentDrawer = PermanentDrawer;
Drawer.PersistentDrawer = PersistentDrawer;
var _default = Drawer;
exports.default = _default;