@cypress-design/react-tooltip 0.2.0 → 0.3.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @cypress-design/react-tooltip
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#58](https://github.com/cypress-io/cypress-design/pull/58) [`ee05fa5`](https://github.com/cypress-io/cypress-design/commit/ee05fa5bf0caddf16521636ce7a26a19a5103025) Thanks [@elevatebart](https://github.com/elevatebart)! - Add interactive & forcePlacement props
8
+
3
9
  ## 0.2.0
4
10
 
5
11
  ### Minor Changes
@@ -24,6 +24,26 @@ export default ({
24
24
  >
25
25
  <div>Hover Me (dynamic: {placement?.toString()})</div>
26
26
  </Tooltip>
27
+ <Tooltip
28
+ className="bg-red-200 p-4 w-1/2"
29
+ popper={
30
+ <div className="h-200px flex items-center justify-center">Popover</div>
31
+ }
32
+ color={color}
33
+ placement="top"
34
+ forcePlacement
35
+ >
36
+ <div>Force Placement to the top</div>
37
+ </Tooltip>
38
+ <Tooltip
39
+ className="bg-red-200 p-4 w-1/2"
40
+ popper={<div>Popover</div>}
41
+ color={color}
42
+ placement={placement}
43
+ interactive
44
+ >
45
+ <div>Interactive {placement?.toString()}</div>
46
+ </Tooltip>
27
47
  {(['top', 'right', 'bottom', 'left', 'top-start'] as const).map(
28
48
  (placement) => (
29
49
  <Tooltip
package/Tooltip.tsx CHANGED
@@ -37,6 +37,14 @@ export interface TooltipProps {
37
37
  * This hides the popper and makes the tooltip inactive.
38
38
  */
39
39
  disabled?: boolean
40
+ /**
41
+ * If true, the tooltip will be hidden when hovering the popper/tooltip
42
+ */
43
+ interactive?: boolean
44
+ /**
45
+ * If set, the tooltip will always respect the given placement
46
+ */
47
+ forcePlacement?: boolean
40
48
  }
41
49
 
42
50
  const ROTATE_MAP = {
@@ -55,6 +63,8 @@ export const Tooltip: React.FC<
55
63
  children,
56
64
  popper,
57
65
  disabled,
66
+ interactive,
67
+ forcePlacement,
58
68
  ...rest
59
69
  }) => {
60
70
  const arrowRef = React.useRef(null)
@@ -86,13 +96,22 @@ export const Tooltip: React.FC<
86
96
  placement,
87
97
  open,
88
98
  onOpenChange: setOpen,
89
- middleware: [flip(), offset(0), arrow({ element: arrowRef, padding: 24 })],
99
+ middleware: [
100
+ forcePlacement
101
+ ? {
102
+ name: 'no-flip',
103
+ fn: (obj) => obj,
104
+ }
105
+ : flip(),
106
+ offset(0),
107
+ arrow({ element: arrowRef, padding: 24 }),
108
+ ],
90
109
  whileElementsMounted: autoUpdate,
91
110
  })
92
111
 
93
112
  const { getReferenceProps, getFloatingProps } = useInteractions([
94
113
  useHover(context, {
95
- handleClose: safePolygon(),
114
+ handleClose: interactive ? safePolygon() : undefined,
96
115
  enabled: !disabled,
97
116
  }),
98
117
  useFocus(context, {
@@ -121,6 +140,27 @@ export const Tooltip: React.FC<
121
140
  arrowYRule = 'top'
122
141
  }
123
142
 
143
+ const getColors = () => {
144
+ switch (color) {
145
+ case 'light':
146
+ return {
147
+ svg: 'stroke-gray-100 fill-white',
148
+ block: 'text-gray-900 border-gray-100 shadow-gray-100',
149
+ background: 'bg-white',
150
+ }
151
+ case 'dark':
152
+ return {
153
+ svg: 'stroke-gray-800 fill-gray-900',
154
+ block: 'text-white shadow-gray-800 border-gray-800',
155
+ background: 'bg-gray-900',
156
+ }
157
+ default:
158
+ return {}
159
+ }
160
+ }
161
+
162
+ const colors = getColors()
163
+
124
164
  return (
125
165
  <>
126
166
  <div
@@ -144,45 +184,44 @@ export const Tooltip: React.FC<
144
184
  },
145
185
  })}
146
186
  >
147
- <svg
148
- ref={arrowRef}
149
- viewBox="0 0 48 48"
150
- width="24"
151
- height="24"
152
- className={clsx('absolute', [
153
- color === 'dark' && 'stroke-gray-800 fill-gray-900',
154
- color === 'light' && 'stroke-gray-100 fill-white',
155
- ])}
156
- style={{
157
- transform: `rotate(${arrowRotate}deg)`,
158
- filter:
159
- placementSide === 'bottom' || color === 'dark'
160
- ? undefined
161
- : 'drop-shadow(0 1px 1px rgba(225, 227, 237, .8))',
162
- [arrowXRule]: `${arrowX ?? -6}px`,
163
- [arrowYRule]: `${arrowY ?? -6}px`,
164
- }}
165
- fill="none"
166
- >
167
- <rect x="0" y="0" width="48" height="4" strokeWidth="0" />
168
- <path
169
- d="M 0 3 C 12 3 18 18 24 18 C 30 18 36 3 48 3"
170
- stroke-width="2"
171
- />
172
- </svg>
173
187
  <div
174
- className={clsx(
175
- 'rounded shadow border p-8px text-16px leading-24px min-w-160px text-center',
176
- [
177
- color === 'dark' &&
178
- 'bg-gray-900 shadow-gray-800 border-gray-800',
179
- color === 'light' &&
180
- 'bg-white shadow-gray-100 border-gray-100',
181
- (!x || !y) && 'invisible',
182
- ]
183
- )}
188
+ className={clsx('rounded shadow border', [
189
+ colors.background,
190
+ colors.block,
191
+ (!x || !y) && 'invisible',
192
+ ])}
184
193
  >
185
- {popper}
194
+ <svg
195
+ ref={arrowRef}
196
+ viewBox="0 0 48 48"
197
+ width="24"
198
+ height="24"
199
+ className={clsx('absolute z-10', colors.svg)}
200
+ style={{
201
+ transform: `rotate(${arrowRotate}deg)`,
202
+ filter:
203
+ placementSide === 'bottom' || color === 'dark'
204
+ ? undefined
205
+ : 'drop-shadow(0 1px 1px rgba(225, 227, 237, .8))',
206
+ [arrowXRule]: `${arrowX ?? -6}px`,
207
+ [arrowYRule]: `${arrowY ?? -6}px`,
208
+ }}
209
+ fill="none"
210
+ >
211
+ <rect x="0" y="0" width="48" height="4" strokeWidth="0" />
212
+ <path
213
+ d="M 0 3 C 12 3 18 18 24 18 C 30 18 36 3 48 3"
214
+ strokeWidth="2"
215
+ />
216
+ </svg>
217
+ <div
218
+ className={clsx(
219
+ 'rounded text-16px leading-24px min-w-160px text-center p-8px relative z-20',
220
+ colors.background
221
+ )}
222
+ >
223
+ {popper}
224
+ </div>
186
225
  </div>
187
226
  </div>
188
227
  )}
package/dist/Tooltip.d.ts CHANGED
@@ -21,6 +21,14 @@ export interface TooltipProps {
21
21
  * This hides the popper and makes the tooltip inactive.
22
22
  */
23
23
  disabled?: boolean;
24
+ /**
25
+ * If true, the tooltip will be hidden when hovering the popper/tooltip
26
+ */
27
+ interactive?: boolean;
28
+ /**
29
+ * If set, the tooltip will always respect the given placement
30
+ */
31
+ forcePlacement?: boolean;
24
32
  }
25
33
  export declare const Tooltip: React.FC<TooltipProps & React.HTMLProps<HTMLDivElement>>;
26
34
  export default Tooltip;
@@ -1 +1 @@
1
- {"version":3,"file":"Tooltip.d.ts","sourceRoot":"","sources":["../Tooltip.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EACL,SAAS,EAaV,MAAM,qCAAqC,CAAA;AAE5C,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,GAAG,MAAM,CAAA;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IAC1B,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;IAC1B;;OAEG;IACH,MAAM,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;IACxB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AASD,eAAO,MAAM,OAAO,EAAE,KAAK,CAAC,EAAE,CAC5B,YAAY,GAAG,KAAK,CAAC,SAAS,CAAC,cAAc,CAAC,CA8I/C,CAAA;AAED,eAAe,OAAO,CAAA"}
1
+ {"version":3,"file":"Tooltip.d.ts","sourceRoot":"","sources":["../Tooltip.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EACL,SAAS,EAaV,MAAM,qCAAqC,CAAA;AAE5C,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,GAAG,MAAM,CAAA;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IAC1B,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;IAC1B;;OAEG;IACH,MAAM,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;IACxB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAA;CACzB;AASD,eAAO,MAAM,OAAO,EAAE,KAAK,CAAC,EAAE,CAC5B,YAAY,GAAG,KAAK,CAAC,SAAS,CAAC,cAAc,CAAC,CA6K/C,CAAA;AAED,eAAe,OAAO,CAAA"}
package/dist/index.es.js CHANGED
@@ -30316,7 +30316,7 @@ var ROTATE_MAP = {
30316
30316
  };
30317
30317
  var Tooltip = function (_a) {
30318
30318
  var _b;
30319
- var placement = _a.placement, _c = _a.color, color = _c === void 0 ? 'light' : _c, className = _a.className, children = _a.children, popper = _a.popper, disabled = _a.disabled, rest = __rest(_a, ["placement", "color", "className", "children", "popper", "disabled"]);
30319
+ var placement = _a.placement, _c = _a.color, color = _c === void 0 ? 'light' : _c, className = _a.className, children = _a.children, popper = _a.popper, disabled = _a.disabled, interactive = _a.interactive, forcePlacement = _a.forcePlacement, rest = __rest(_a, ["placement", "color", "className", "children", "popper", "disabled", "interactive", "forcePlacement"]);
30320
30320
  var arrowRef = React.useRef(null);
30321
30321
  var _d = React.useState(false), open = _d[0], setOpen = _d[1];
30322
30322
  var container = React.useState(function () {
@@ -30334,12 +30334,21 @@ var Tooltip = function (_a) {
30334
30334
  placement: placement,
30335
30335
  open: open,
30336
30336
  onOpenChange: setOpen,
30337
- middleware: [flip(), offset(0), arrow({ element: arrowRef, padding: 24 })],
30337
+ middleware: [
30338
+ forcePlacement
30339
+ ? {
30340
+ name: 'no-flip',
30341
+ fn: function (obj) { return obj; }
30342
+ }
30343
+ : flip(),
30344
+ offset(0),
30345
+ arrow({ element: arrowRef, padding: 24 }),
30346
+ ],
30338
30347
  whileElementsMounted: autoUpdate
30339
30348
  }), x = _e.x, y = _e.y, reference = _e.reference, floating = _e.floating, strategy = _e.strategy, calculatedPlacement = _e.placement, context = _e.context, _f = _e.middlewareData.arrow, _g = _f === void 0 ? {} : _f, arrowX = _g.x, arrowY = _g.y;
30340
30349
  var _h = useInteractions([
30341
30350
  useHover(context, {
30342
- handleClose: safePolygon(),
30351
+ handleClose: interactive ? safePolygon() : undefined,
30343
30352
  enabled: !disabled
30344
30353
  }),
30345
30354
  useFocus(context, {
@@ -30363,6 +30372,25 @@ var Tooltip = function (_a) {
30363
30372
  arrowXRule = placementSide === 'left' ? 'right' : 'left';
30364
30373
  arrowYRule = 'top';
30365
30374
  }
30375
+ var getColors = function () {
30376
+ switch (color) {
30377
+ case 'light':
30378
+ return {
30379
+ svg: 'stroke-gray-100 fill-white',
30380
+ block: 'text-gray-900 border-gray-100 shadow-gray-100',
30381
+ background: 'bg-white'
30382
+ };
30383
+ case 'dark':
30384
+ return {
30385
+ svg: 'stroke-gray-800 fill-gray-900',
30386
+ block: 'text-white shadow-gray-800 border-gray-800',
30387
+ background: 'bg-gray-900'
30388
+ };
30389
+ default:
30390
+ return {};
30391
+ }
30392
+ };
30393
+ var colors = getColors();
30366
30394
  return (React.createElement(React.Fragment, null,
30367
30395
  React.createElement("div", __assign({}, getReferenceProps(), { ref: reference, className: className }, rest), children),
30368
30396
  React.createElement(FloatingPortal, null, open && !disabled && (React.createElement("div", __assign({}, getFloatingProps({
@@ -30374,30 +30402,26 @@ var Tooltip = function (_a) {
30374
30402
  padding: '16px'
30375
30403
  }
30376
30404
  })),
30377
- React.createElement("svg", { ref: arrowRef, viewBox: "0 0 48 48", width: "24", height: "24", className: clsx('absolute', [
30378
- color === 'dark' && 'stroke-gray-800 fill-gray-900',
30379
- color === 'light' && 'stroke-gray-100 fill-white',
30380
- ]), style: (_b = {
30381
- transform: "rotate(" + arrowRotate + "deg)",
30382
- filter: placementSide === 'bottom' || color === 'dark'
30383
- ? undefined
30384
- : 'drop-shadow(0 1px 1px rgba(225, 227, 237, .8))'
30385
- },
30386
- _b[arrowXRule] = (arrowX !== null && arrowX !== void 0 ? arrowX : -6) + "px",
30387
- _b[arrowYRule] = (arrowY !== null && arrowY !== void 0 ? arrowY : -6) + "px",
30388
- _b), fill: "none" },
30389
- React.createElement("rect", { x: "0", y: "0", width: "48", height: "4", strokeWidth: "0" }),
30390
- React.createElement("path", { d: "M 0 3 C 12 3 18 18 24 18 C 30 18 36 3 48 3", "stroke-width": "2" })),
30391
- React.createElement("div", { className: clsx('rounded shadow border p-8px text-16px leading-24px min-w-160px text-center', [
30392
- color === 'dark' &&
30393
- 'bg-gray-900 shadow-gray-800 border-gray-800',
30394
- color === 'light' &&
30395
- 'bg-white shadow-gray-100 border-gray-100',
30405
+ React.createElement("div", { className: clsx('rounded shadow border', [
30406
+ colors.background,
30407
+ colors.block,
30396
30408
  (!x || !y) && 'invisible',
30397
- ]) }, popper))))));
30409
+ ]) },
30410
+ React.createElement("svg", { ref: arrowRef, viewBox: "0 0 48 48", width: "24", height: "24", className: clsx('absolute z-10', colors.svg), style: (_b = {
30411
+ transform: "rotate(" + arrowRotate + "deg)",
30412
+ filter: placementSide === 'bottom' || color === 'dark'
30413
+ ? undefined
30414
+ : 'drop-shadow(0 1px 1px rgba(225, 227, 237, .8))'
30415
+ },
30416
+ _b[arrowXRule] = (arrowX !== null && arrowX !== void 0 ? arrowX : -6) + "px",
30417
+ _b[arrowYRule] = (arrowY !== null && arrowY !== void 0 ? arrowY : -6) + "px",
30418
+ _b), fill: "none" },
30419
+ React.createElement("rect", { x: "0", y: "0", width: "48", height: "4", strokeWidth: "0" }),
30420
+ React.createElement("path", { d: "M 0 3 C 12 3 18 18 24 18 C 30 18 36 3 48 3", strokeWidth: "2" })),
30421
+ React.createElement("div", { className: clsx('rounded text-16px leading-24px min-w-160px text-center p-8px relative z-20', colors.background) }, popper)))))));
30398
30422
  };
30399
30423
 
30400
30424
  export { Tooltip as default };
30401
- /* <windicss-keep class="container absolute stroke-gray-800 fill-gray-900 stroke-gray-100 fill-white rounded shadow border p-8px text-16px leading-24px min-w-160px text-center bg-gray-900 shadow-gray-800 border-gray-800 bg-white shadow-gray-100 border-gray-100 invisible"> */
30425
+ /* <windicss-keep class="container stroke-gray-100 fill-white text-gray-900 border-gray-100 shadow-gray-100 bg-white stroke-gray-800 fill-gray-900 text-white shadow-gray-800 border-gray-800 bg-gray-900 rounded shadow border invisible absolute z-10 text-16px leading-24px min-w-160px text-center p-8px relative z-20"> */
30402
30426
 
30403
30427
  //# sourceMappingURL=index.es.js.map