@micromag/editor 0.3.498 → 0.3.500

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +49 -29
  2. package/package.json +5 -5
package/lib/index.js CHANGED
@@ -198,7 +198,7 @@ if (typeof history !== "undefined" && typeof window[patchKey] === "undefined") {
198
198
 
199
199
  var _excluded$d = ["children"],
200
200
  _excluded2$1 = ["path", "nest", "match"],
201
- _excluded3 = ["to", "href", "onClick", "asChild", "children", "replace", "state"];
201
+ _excluded3 = ["to", "href", "onClick", "asChild", "children", "className", "replace", "state"];
202
202
 
203
203
  /*
204
204
  * Transforms `path` into its relative `base` version
@@ -241,7 +241,11 @@ var defaultRouter = {
241
241
  base: "",
242
242
  // this option is used to override the current location during SSR
243
243
  ssrPath: undefined,
244
- ssrSearch: undefined
244
+ ssrSearch: undefined,
245
+ // customizes how `href` props are transformed for <Link />
246
+ hrefs: function hrefs(x) {
247
+ return x;
248
+ }
245
249
  };
246
250
  var RouterCtx = React.createContext(defaultRouter);
247
251
 
@@ -305,6 +309,7 @@ var matchRoute = function matchRoute(parser, route, path, loose) {
305
309
  */
306
310
 
307
311
  var Router = function Router(_ref3) {
312
+ var _props$ssrPath$split, _props$ssrPath, _props$hrefs, _props$hook;
308
313
  var children = _ref3.children,
309
314
  props = _objectWithoutProperties(_ref3, _excluded$d);
310
315
  // the router we will inherit from - it is the closest router in the tree,
@@ -315,6 +320,16 @@ var Router = function Router(_ref3) {
315
320
  // holds to the context value: the router object
316
321
  var value = parent;
317
322
 
323
+ // when `ssrPath` contains a `?` character, we can extract the search from it
324
+ var _ref4 = (_props$ssrPath$split = (_props$ssrPath = props.ssrPath) === null || _props$ssrPath === void 0 ? void 0 : _props$ssrPath.split("?")) !== null && _props$ssrPath$split !== void 0 ? _props$ssrPath$split : [],
325
+ _ref5 = _slicedToArray(_ref4, 2),
326
+ path = _ref5[0],
327
+ search = _ref5[1];
328
+ if (search) props.ssrSearch = search, props.ssrPath = path;
329
+
330
+ // hooks can define their own `href` formatter (e.g. for hash location)
331
+ props.hrefs = (_props$hrefs = props.hrefs) !== null && _props$hrefs !== void 0 ? _props$hrefs : (_props$hook = props.hook) === null || _props$hook === void 0 ? void 0 : _props$hook.hrefs;
332
+
318
333
  // what is happening below: to avoid unnecessary rerenders in child components,
319
334
  // we ensure that the router object reference is stable, unless there are any
320
335
  // changes that require reload (e.g. `base` prop changes -> all components that
@@ -344,9 +359,9 @@ var Router = function Router(_ref3) {
344
359
  children: children
345
360
  });
346
361
  };
347
- var h_route = function h_route(_ref4, params) {
348
- var children = _ref4.children,
349
- component = _ref4.component;
362
+ var h_route = function h_route(_ref6, params) {
363
+ var children = _ref6.children,
364
+ component = _ref6.component;
350
365
  // React-Router style `component` prop
351
366
  if (component) return React.createElement(component, {
352
367
  params: params
@@ -355,22 +370,22 @@ var h_route = function h_route(_ref4, params) {
355
370
  // support render prop or plain children
356
371
  return typeof children === "function" ? children(params) : children;
357
372
  };
358
- var Route = function Route(_ref5) {
359
- var path = _ref5.path,
360
- nest = _ref5.nest,
361
- match = _ref5.match,
362
- renderProps = _objectWithoutProperties(_ref5, _excluded2$1);
373
+ var Route = function Route(_ref7) {
374
+ var path = _ref7.path,
375
+ nest = _ref7.nest,
376
+ match = _ref7.match,
377
+ renderProps = _objectWithoutProperties(_ref7, _excluded2$1);
363
378
  var router = useRouter();
364
379
  var _useLocationFromRoute = useLocationFromRouter(router),
365
380
  _useLocationFromRoute2 = _slicedToArray(_useLocationFromRoute, 1),
366
381
  location = _useLocationFromRoute2[0];
367
- var _ref6 = // `match` is a special prop to give up control to the parent,
382
+ var _ref8 = // `match` is a special prop to give up control to the parent,
368
383
  // it is used by the `Switch` to avoid double matching
369
384
  match !== null && match !== void 0 ? match : matchRoute(router.parser, path, location, nest),
370
- _ref7 = _slicedToArray(_ref6, 3),
371
- matches = _ref7[0],
372
- params = _ref7[1],
373
- base = _ref7[2];
385
+ _ref9 = _slicedToArray(_ref8, 3),
386
+ matches = _ref9[0],
387
+ params = _ref9[1],
388
+ base = _ref9[2];
374
389
  if (!matches) return null;
375
390
  var children = base ? React.createElement(Router, {
376
391
  base: base
@@ -384,13 +399,15 @@ React.forwardRef(function (props, ref) {
384
399
  var router = useRouter();
385
400
  var _useLocationFromRoute3 = useLocationFromRouter(router),
386
401
  _useLocationFromRoute4 = _slicedToArray(_useLocationFromRoute3, 2),
402
+ path = _useLocationFromRoute4[0],
387
403
  navigate = _useLocationFromRoute4[1];
388
404
  var to = props.to,
389
405
  _props$href = props.href,
390
406
  _href = _props$href === void 0 ? to : _props$href,
391
407
  _onClick = props.onClick,
392
408
  asChild = props.asChild,
393
- children = props.children;
409
+ children = props.children,
410
+ cls = props.className;
394
411
  props.replace;
395
412
  props.state;
396
413
  var restProps = _objectWithoutProperties(props, _excluded3);
@@ -398,7 +415,7 @@ React.forwardRef(function (props, ref) {
398
415
  // ignores the navigation when clicked using right mouse button or
399
416
  // by holding a special modifier key: ctrl, command, win, alt, shift
400
417
  if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey || event.button !== 0) return;
401
- _onClick && _onClick(event); // TODO: is it safe to use _onClick?.(event)
418
+ _onClick === null || _onClick === void 0 || _onClick(event);
402
419
  if (!event.defaultPrevented) {
403
420
  event.preventDefault();
404
421
  navigate(_href, props);
@@ -406,26 +423,28 @@ React.forwardRef(function (props, ref) {
406
423
  });
407
424
 
408
425
  // handle nested routers and absolute paths
409
- var href = _href[0] === "~" ? _href.slice(1) : router.base + _href;
426
+ var href = router.hrefs(_href[0] === "~" ? _href.slice(1) : router.base + _href, router // pass router as a second argument for convinience
427
+ );
410
428
  return asChild && React.isValidElement(children) ? React.cloneElement(children, {
411
- href: href,
412
- onClick: onClick
429
+ onClick: onClick,
430
+ href: href
413
431
  }) : React.createElement("a", _objectSpread(_objectSpread({}, restProps), {}, {
414
- href: href,
415
432
  onClick: onClick,
433
+ href: href,
434
+ // `className` can be a function to apply the class if this link is active
435
+ className: cls !== null && cls !== void 0 && cls.call ? cls(path === href) : cls,
416
436
  children: children,
417
437
  ref: ref
418
438
  }));
419
439
  });
420
440
  var flattenChildren = function flattenChildren(children) {
421
- var _ref8;
422
- return Array.isArray(children) ? (_ref8 = []).concat.apply(_ref8, _toConsumableArray(children.map(function (c) {
423
- return c && c.type === React.Fragment ? flattenChildren(c.props.children) : flattenChildren(c);
424
- }))) : [children];
441
+ return Array.isArray(children) ? children.flatMap(function (c) {
442
+ return flattenChildren(c && c.type === React.Fragment ? c.props.children : c);
443
+ }) : [children];
425
444
  };
426
- var Switch = function Switch(_ref9) {
427
- var children = _ref9.children,
428
- location = _ref9.location;
445
+ var Switch = function Switch(_ref10) {
446
+ var children = _ref10.children,
447
+ location = _ref10.location;
429
448
  var router = useRouter();
430
449
  var _useLocationFromRoute5 = useLocationFromRouter(router),
431
450
  _useLocationFromRoute6 = _slicedToArray(_useLocationFromRoute5, 1),
@@ -550,7 +569,8 @@ function useRouteMatcher() {
550
569
  return matcher;
551
570
  }
552
571
  function createPathToRegexpParser(opts) {
553
- return function (path, loose) {
572
+ return function (fullPath, loose) {
573
+ var path = fullPath.replace(/^(https?:\/\/[^/]+)\/?/, '/');
554
574
  var keys = [];
555
575
  var isWildcard = path.match(/(\/|^)\*$/) !== null;
556
576
  var pattern = pathToRegexp.pathToRegexp(isWildcard ? path.replace(/(\/|^)\*$/, '$1(.*)') : path, keys, _objectSpread({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micromag/editor",
3
- "version": "0.3.498",
3
+ "version": "0.3.500",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [
@@ -73,10 +73,10 @@
73
73
  "@fortawesome/free-solid-svg-icons": "^5.15.1",
74
74
  "@fortawesome/react-fontawesome": "^0.1.13",
75
75
  "@micromag/core": "^0.3.497",
76
- "@micromag/elements": "^0.3.497",
76
+ "@micromag/elements": "^0.3.500",
77
77
  "@micromag/fields": "^0.3.497",
78
- "@micromag/screens": "^0.3.497",
79
- "@micromag/viewer": "^0.3.498",
78
+ "@micromag/screens": "^0.3.500",
79
+ "@micromag/viewer": "^0.3.500",
80
80
  "classnames": "^2.2.6",
81
81
  "lodash": "^4.17.21",
82
82
  "prop-types": "^15.7.2",
@@ -91,5 +91,5 @@
91
91
  "access": "public",
92
92
  "registry": "https://registry.npmjs.org/"
93
93
  },
94
- "gitHead": "0241852143b11b4cd1d6e7abf1b05b9c73513692"
94
+ "gitHead": "ec754f9301e9fbd5c7ee1e929acb5355c732cfd6"
95
95
  }