UNPKG

preact-enroute

Version:

Small preact router (preact port of tj/react-enroute)

160 lines (121 loc) 5.2 kB
import { Component, h } from 'preact'; import enroute from 'enroute'; var _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; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function assert(e, msg) { if (!e) { throw new Error('preact-enroute: ' + msg); } } /** * Router routes things. */ var Router = function (_Component) { _inherits(Router, _Component); /** * Initialize the router. */ function Router(props) { _classCallCheck(this, Router); var _this = _possibleConstructorReturn(this, (Router.__proto__ || Object.getPrototypeOf(Router)).call(this, props)); _this.routes = {}; _this.addRoutes(props.children); _this.router = enroute(_this.routes); return _this; } /** * Add routes. */ _createClass(Router, [{ key: 'addRoutes', value: function addRoutes(routes, parent) { var _this2 = this; routes.forEach(function (r) { return _this2.addRoute(r, parent); }); } /** * Add route. */ }, { key: 'addRoute', value: function addRoute(el, parent) { var _props = this.props, location = _props.location, props = _objectWithoutProperties(_props, ['location']); var _el$attributes = el.attributes, path = _el$attributes.path, component = _el$attributes.component; var children = el.children; assert(typeof path === 'string', 'Route ' + context(el.attributes) + 'is missing the "path" property'); assert(component, 'Route ' + context(el.attributes) + 'is missing the "component" property'); function render(params, renderProps) { var finalProps = _extends({}, props, renderProps, { location: location, params: params }); var children = h(component, finalProps); return parent ? parent.render(params, { children: children }) : children; } var route = normalizeRoute(path, parent); if (children) { this.addRoutes(children, { route: route, render: render }); } this.routes[cleanPath(route)] = render; } /** * Render the matching route. */ }, { key: 'render', value: function render() { var location = this.props.location; assert(location, 'Router "location" property is missing'); return this.router(location, { children: null }); } }]); return Router; }(Component); /** * Route does absolutely nothing :). */ function Route() { assert(false, 'Route should not be rendered'); } /** * Context string for route errors based on the props available. */ function context(_ref) { var path = _ref.path, component = _ref.component; if (path) { return 'with path "' + path + '" '; } if (component) { return 'with component ' + component.name + ' '; } return ''; } /** * Normalize route based on the parent. */ function normalizeRoute(path, parent) { if (path[0] === '/' || path[0] === '') { return path; // absolute route } if (!parent) { return path; // no need for a join } return parent.route + '/' + path; // join } /** * Clean path by stripping subsequent "//"'s. Without this * the user must be careful when to use "/" or not, which leads * to bad UX. */ function cleanPath(path) { return path.replace(/\/\//g, '/'); } export { Router, Route }; //# sourceMappingURL=es.js.map