reshaped 2.11.10 → 2.11.11

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.
@@ -0,0 +1,97 @@
1
+ const SCREEN_OFFSET = 16;
2
+ const getRTLPosition = (position) => {
3
+ if (position.includes("start"))
4
+ return position.replace("start", "end");
5
+ if (position.includes("end"))
6
+ return position.replace("end", "start");
7
+ return position;
8
+ };
9
+ /**
10
+ * Get a position value which centers 2 elements vertically or horizontally
11
+ */
12
+ const centerBySize = (originSize, targetSize) => {
13
+ return Math.floor(originSize / 2 - targetSize / 2);
14
+ };
15
+ /**
16
+ * Calculate styles for the current position
17
+ */
18
+ const calculatePosition = (args) => {
19
+ const { triggerBounds, flyoutBounds, scopeOffset, position: passedPosition, rtl, width } = args;
20
+ let left = 0;
21
+ let top = 0;
22
+ let position = passedPosition;
23
+ if (rtl)
24
+ position = getRTLPosition(position);
25
+ if (width === "full" || width === "trigger") {
26
+ position = position.includes("top") ? "top" : "bottom";
27
+ }
28
+ switch (position) {
29
+ case "bottom":
30
+ case "top":
31
+ left = centerBySize(triggerBounds.width, flyoutBounds.width) + triggerBounds.left;
32
+ break;
33
+ case "start":
34
+ case "start-top":
35
+ case "start-bottom":
36
+ left = triggerBounds.left - triggerBounds.width;
37
+ break;
38
+ case "end":
39
+ case "end-top":
40
+ case "end-bottom":
41
+ left = triggerBounds.right;
42
+ break;
43
+ case "top-start":
44
+ case "bottom-start":
45
+ left = triggerBounds.left;
46
+ break;
47
+ case "top-end":
48
+ case "bottom-end":
49
+ left = triggerBounds.right - flyoutBounds.width;
50
+ break;
51
+ default:
52
+ break;
53
+ }
54
+ switch (position) {
55
+ case "top":
56
+ case "top-start":
57
+ case "top-end":
58
+ top = triggerBounds.top - flyoutBounds.height;
59
+ break;
60
+ case "bottom":
61
+ case "bottom-start":
62
+ case "bottom-end":
63
+ top = triggerBounds.bottom;
64
+ break;
65
+ case "start":
66
+ case "end":
67
+ top = centerBySize(triggerBounds.height, flyoutBounds.height) + triggerBounds.top;
68
+ break;
69
+ case "start-top":
70
+ case "end-top":
71
+ top = triggerBounds.top;
72
+ break;
73
+ case "start-bottom":
74
+ case "end-bottom":
75
+ top = triggerBounds.bottom - triggerBounds.height;
76
+ break;
77
+ default:
78
+ break;
79
+ }
80
+ if (top === undefined || left === undefined) {
81
+ throw Error(`[Reshaped, flyout]: ${position} position is not valid`);
82
+ }
83
+ top = Math.round(top + (window.scrollY || 0) - scopeOffset.top);
84
+ left = Math.round(left + (window.scrollX || 0) - scopeOffset.left);
85
+ let widthStyle = Math.ceil(flyoutBounds.width);
86
+ const height = Math.ceil(flyoutBounds.height);
87
+ if (width === "full") {
88
+ left = SCREEN_OFFSET;
89
+ widthStyle = window.innerWidth - SCREEN_OFFSET * 2;
90
+ }
91
+ else if (width === "trigger") {
92
+ widthStyle = triggerBounds.width;
93
+ }
94
+ const styles = { left, top, width: widthStyle, height };
95
+ return { styles, position };
96
+ };
97
+ export default calculatePosition;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "reshaped",
3
3
  "description": "Professionally crafted design system in React & Figma for building products of any scale and complexity",
4
- "version": "2.11.10",
4
+ "version": "2.11.11",
5
5
  "license": "MIT",
6
6
  "email": "hello@reshaped.so",
7
7
  "homepage": "https://reshaped.so",
@@ -7,6 +7,7 @@ type ReleaseOptions = {
7
7
  type TrapOptions = {
8
8
  onNavigateOutside?: () => void;
9
9
  includeTrigger?: boolean;
10
+ initialFocusEl?: FocusableElement;
10
11
  mode?: TrapMode;
11
12
  };
12
13
  declare class TrapFocus {
@@ -60,7 +60,7 @@ class TrapFocus {
60
60
  * and create a chain item
61
61
  */
62
62
  this.trap = (options = {}) => {
63
- const { mode = "dialog", includeTrigger } = options;
63
+ const { mode = "dialog", includeTrigger, initialFocusEl } = options;
64
64
  const trigger = getActiveElement();
65
65
  const focusable = getFocusableElements(this.root, {
66
66
  additionalElement: includeTrigger ? trigger : undefined,
@@ -84,14 +84,14 @@ class TrapFocus {
84
84
  if (mode === "dialog")
85
85
  this.screenReaderTrap.trap();
86
86
  this.mutationObserver.observe(this.root, { childList: true, subtree: true });
87
- if (!focusable.length)
87
+ if (!focusable.length && !initialFocusEl)
88
88
  return;
89
89
  this.addListeners();
90
90
  // Don't add back to the chain if we're traversing back
91
91
  const tailItem = TrapFocus.chain.tailId && TrapFocus.chain.get(TrapFocus.chain.tailId);
92
92
  if (!tailItem || this.root !== tailItem.data.root) {
93
93
  this.chainId = TrapFocus.chain.add(this);
94
- focusElement(focusable[0], { pseudoFocus });
94
+ focusElement(initialFocusEl || focusable[0], { pseudoFocus });
95
95
  }
96
96
  this.trapped = true;
97
97
  };
@@ -1,27 +0,0 @@
1
- import React from "react";
2
- /**
3
- * Typings
4
- */
5
- export type FlyoutPosition = "bottom" | "bottom-start" | "bottom-end" | "top" | "top-start" | "top-end" | "start" | "start-top" | "start-bottom" | "end" | "end-top" | "end-bottom";
6
- export type FlyoutWidth = "trigger" | string;
7
- type ElementRef = React.RefObject<HTMLElement>;
8
- type PassedFlyoutOptions = {
9
- width?: FlyoutWidth;
10
- position?: FlyoutPosition;
11
- defaultActive?: boolean;
12
- forcePosition?: boolean;
13
- };
14
- type FlyoutStyles = React.CSSProperties;
15
- type FlyoutState = {
16
- styles: FlyoutStyles;
17
- position?: FlyoutPosition;
18
- status: "idle" | "rendered" | "positioned" | "visible" | "hidden";
19
- };
20
- type UseFlyout = (originRef: ElementRef, targetRef: ElementRef, options: PassedFlyoutOptions) => Pick<FlyoutState, "styles" | "position" | "status"> & {
21
- updatePosition: () => void;
22
- render: () => void;
23
- hide: () => void;
24
- remove: () => void;
25
- };
26
- declare const useFlyout: UseFlyout;
27
- export default useFlyout;