@cypress-design/react-tooltip 0.0.2 → 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,26 @@
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
+
9
+ ## 0.2.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#53](https://github.com/cypress-io/cypress-design/pull/53) [`a9a616b`](https://github.com/cypress-io/cypress-design/commit/a9a616bc4684252fcd71e4a82d2141bdfbea32bc) Thanks [@elevatebart](https://github.com/elevatebart)! - - add a default font color when light and dark mode are used
14
+ - add disabled prop to tooltip
15
+
16
+ * [#51](https://github.com/cypress-io/cypress-design/pull/51) [`ccd8f9a`](https://github.com/cypress-io/cypress-design/commit/ccd8f9a8feb624c0a52deaa9754c76969f43fc1e) Thanks [@elevatebart](https://github.com/elevatebart)! - in the build asset, add a comment containing all used windicss classes in the component
17
+
18
+ ## 0.1.0
19
+
20
+ ### Minor Changes
21
+
22
+ - [#44](https://github.com/cypress-io/cypress-design/pull/44) [`2fdaf6b`](https://github.com/cypress-io/cypress-design/commit/2fdaf6be6f81a2a851761258347ed213577c5b26) Thanks [@elevatebart](https://github.com/elevatebart)! - Add missing descriptions of props in button, tooltip, checkbox
23
+
3
24
  ## 0.0.2
4
25
 
5
26
  ### Patch Changes
@@ -5,10 +5,12 @@ import Tooltip from './Tooltip'
5
5
  export default ({
6
6
  color = 'light',
7
7
  placement,
8
+ disabled,
8
9
  tabIndex,
9
10
  }: {
10
11
  color?: 'light' | 'dark'
11
12
  placement?: Placement
13
+ disabled?: boolean
12
14
  tabIndex?: number
13
15
  } = {}) => (
14
16
  <div className="center flex flex-col items-center gap-20">
@@ -18,9 +20,30 @@ export default ({
18
20
  color={color}
19
21
  placement={placement}
20
22
  tabIndex={tabIndex}
23
+ disabled={disabled}
21
24
  >
22
25
  <div>Hover Me (dynamic: {placement?.toString()})</div>
23
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>
24
47
  {(['top', 'right', 'bottom', 'left', 'top-start'] as const).map(
25
48
  (placement) => (
26
49
  <Tooltip
@@ -29,6 +52,7 @@ export default ({
29
52
  key={placement}
30
53
  color={color}
31
54
  tabIndex={tabIndex}
55
+ disabled={disabled}
32
56
  popper={
33
57
  <div className="bg-jade-200 text-gray-800 p-4">
34
58
  Popover ({placement})
package/Tooltip.tsx CHANGED
@@ -17,12 +17,34 @@ import {
17
17
  } from '@floating-ui/react-dom-interactions'
18
18
 
19
19
  export interface TooltipProps {
20
+ /**
21
+ * Where the tooltip should be placed relative to the target.
22
+ */
20
23
  placement?: Placement
24
+ /**
25
+ * background color of the tooltip.
26
+ */
21
27
  color?: 'light' | 'dark'
22
28
  className?: string
23
29
  tabIndex?: number | string
24
30
  children?: React.ReactNode
31
+ /**
32
+ * The content of the tooltip.
33
+ */
25
34
  popper?: React.ReactNode
35
+ /**
36
+ * Disable the tooltip.
37
+ * This hides the popper and makes the tooltip inactive.
38
+ */
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
26
48
  }
27
49
 
28
50
  const ROTATE_MAP = {
@@ -34,7 +56,17 @@ const ROTATE_MAP = {
34
56
 
35
57
  export const Tooltip: React.FC<
36
58
  TooltipProps & React.HTMLProps<HTMLDivElement>
37
- > = ({ placement, color = 'light', className, children, popper, ...rest }) => {
59
+ > = ({
60
+ placement,
61
+ color = 'light',
62
+ className,
63
+ children,
64
+ popper,
65
+ disabled,
66
+ interactive,
67
+ forcePlacement,
68
+ ...rest
69
+ }) => {
38
70
  const arrowRef = React.useRef(null)
39
71
  const [open, setOpen] = React.useState(false)
40
72
 
@@ -64,20 +96,34 @@ export const Tooltip: React.FC<
64
96
  placement,
65
97
  open,
66
98
  onOpenChange: setOpen,
67
- 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
+ ],
68
109
  whileElementsMounted: autoUpdate,
69
110
  })
70
111
 
71
112
  const { getReferenceProps, getFloatingProps } = useInteractions([
72
113
  useHover(context, {
73
- handleClose: safePolygon(),
114
+ handleClose: interactive ? safePolygon() : undefined,
115
+ enabled: !disabled,
116
+ }),
117
+ useFocus(context, {
118
+ enabled: !disabled,
74
119
  }),
75
- useFocus(context),
76
120
  useRole(context, { role: 'tooltip' }),
77
121
  useDismiss(context),
78
122
  ])
79
123
 
80
- const placementSide = calculatedPlacement.split('-')[0]
124
+ const placementSide = calculatedPlacement.split(
125
+ '-'
126
+ )[0] as keyof typeof ROTATE_MAP
81
127
  const arrowRotate = ROTATE_MAP[placementSide]
82
128
 
83
129
  let arrowXRule = 'left',
@@ -94,6 +140,27 @@ export const Tooltip: React.FC<
94
140
  arrowYRule = 'top'
95
141
  }
96
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
+
97
164
  return (
98
165
  <>
99
166
  <div
@@ -105,7 +172,7 @@ export const Tooltip: React.FC<
105
172
  {children}
106
173
  </div>
107
174
  <FloatingPortal>
108
- {open && (
175
+ {open && !disabled && (
109
176
  <div
110
177
  {...getFloatingProps({
111
178
  ref: floating,
@@ -117,45 +184,44 @@ export const Tooltip: React.FC<
117
184
  },
118
185
  })}
119
186
  >
120
- <svg
121
- ref={arrowRef}
122
- viewBox="0 0 48 48"
123
- width="24"
124
- height="24"
125
- className={clsx('absolute', [
126
- color === 'dark' && 'stroke-gray-800 fill-gray-900',
127
- color === 'light' && 'stroke-gray-100 fill-white',
128
- ])}
129
- style={{
130
- transform: `rotate(${arrowRotate}deg)`,
131
- filter:
132
- placementSide === 'bottom' || color === 'dark'
133
- ? undefined
134
- : 'drop-shadow(0 1px 1px rgba(225, 227, 237, .8))',
135
- [arrowXRule]: `${arrowX ?? -6}px`,
136
- [arrowYRule]: `${arrowY ?? -6}px`,
137
- }}
138
- fill="none"
139
- >
140
- <rect x="0" y="0" width="48" height="4" strokeWidth="0" />
141
- <path
142
- d="M 0 3 C 12 3 18 18 24 18 C 30 18 36 3 48 3"
143
- stroke-width="2"
144
- />
145
- </svg>
146
187
  <div
147
- className={clsx(
148
- 'rounded shadow border p-8px text-16px leading-24px min-w-160px text-center',
149
- [
150
- color === 'dark' &&
151
- 'bg-gray-900 shadow-gray-800 border-gray-800',
152
- color === 'light' &&
153
- 'bg-white shadow-gray-100 border-gray-100',
154
- (!x || !y) && 'invisible',
155
- ]
156
- )}
188
+ className={clsx('rounded shadow border', [
189
+ colors.background,
190
+ colors.block,
191
+ (!x || !y) && 'invisible',
192
+ ])}
157
193
  >
158
- {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>
159
225
  </div>
160
226
  </div>
161
227
  )}
package/dist/Tooltip.d.ts CHANGED
@@ -1,12 +1,34 @@
1
1
  import * as React from 'react';
2
2
  import { Placement } from '@floating-ui/react-dom-interactions';
3
3
  export interface TooltipProps {
4
+ /**
5
+ * Where the tooltip should be placed relative to the target.
6
+ */
4
7
  placement?: Placement;
8
+ /**
9
+ * background color of the tooltip.
10
+ */
5
11
  color?: 'light' | 'dark';
6
12
  className?: string;
7
13
  tabIndex?: number | string;
8
14
  children?: React.ReactNode;
15
+ /**
16
+ * The content of the tooltip.
17
+ */
9
18
  popper?: React.ReactNode;
19
+ /**
20
+ * Disable the tooltip.
21
+ * This hides the popper and makes the tooltip inactive.
22
+ */
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;
10
32
  }
11
33
  export declare const Tooltip: React.FC<TooltipProps & React.HTMLProps<HTMLDivElement>>;
12
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,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB,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,MAAM,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;CACzB;AASD,eAAO,MAAM,OAAO,EAAE,KAAK,CAAC,EAAE,CAC5B,YAAY,GAAG,KAAK,CAAC,SAAS,CAAC,cAAc,CAAC,CAiI/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, rest = __rest(_a, ["placement", "color", "className", "children", "popper"]);
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,14 +30334,26 @@ 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,
30352
+ enabled: !disabled
30353
+ }),
30354
+ useFocus(context, {
30355
+ enabled: !disabled
30343
30356
  }),
30344
- useFocus(context),
30345
30357
  useRole(context, { role: 'tooltip' }),
30346
30358
  useDismiss(context),
30347
30359
  ]), getReferenceProps = _h.getReferenceProps, getFloatingProps = _h.getFloatingProps;
@@ -30360,9 +30372,28 @@ var Tooltip = function (_a) {
30360
30372
  arrowXRule = placementSide === 'left' ? 'right' : 'left';
30361
30373
  arrowYRule = 'top';
30362
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();
30363
30394
  return (React.createElement(React.Fragment, null,
30364
30395
  React.createElement("div", __assign({}, getReferenceProps(), { ref: reference, className: className }, rest), children),
30365
- React.createElement(FloatingPortal, null, open && (React.createElement("div", __assign({}, getFloatingProps({
30396
+ React.createElement(FloatingPortal, null, open && !disabled && (React.createElement("div", __assign({}, getFloatingProps({
30366
30397
  ref: floating,
30367
30398
  style: {
30368
30399
  position: strategy,
@@ -30371,28 +30402,26 @@ var Tooltip = function (_a) {
30371
30402
  padding: '16px'
30372
30403
  }
30373
30404
  })),
30374
- React.createElement("svg", { ref: arrowRef, viewBox: "0 0 48 48", width: "24", height: "24", className: clsx('absolute', [
30375
- color === 'dark' && 'stroke-gray-800 fill-gray-900',
30376
- color === 'light' && 'stroke-gray-100 fill-white',
30377
- ]), style: (_b = {
30378
- transform: "rotate(".concat(arrowRotate, "deg)"),
30379
- filter: placementSide === 'bottom' || color === 'dark'
30380
- ? undefined
30381
- : 'drop-shadow(0 1px 1px rgba(225, 227, 237, .8))'
30382
- },
30383
- _b[arrowXRule] = "".concat(arrowX !== null && arrowX !== void 0 ? arrowX : -6, "px"),
30384
- _b[arrowYRule] = "".concat(arrowY !== null && arrowY !== void 0 ? arrowY : -6, "px"),
30385
- _b), fill: "none" },
30386
- React.createElement("rect", { x: "0", y: "0", width: "48", height: "4", strokeWidth: "0" }),
30387
- React.createElement("path", { d: "M 0 3 C 12 3 18 18 24 18 C 30 18 36 3 48 3", "stroke-width": "2" })),
30388
- React.createElement("div", { className: clsx('rounded shadow border p-8px text-16px leading-24px min-w-160px text-center', [
30389
- color === 'dark' &&
30390
- 'bg-gray-900 shadow-gray-800 border-gray-800',
30391
- color === 'light' &&
30392
- 'bg-white shadow-gray-100 border-gray-100',
30405
+ React.createElement("div", { className: clsx('rounded shadow border', [
30406
+ colors.background,
30407
+ colors.block,
30393
30408
  (!x || !y) && 'invisible',
30394
- ]) }, 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)))))));
30395
30422
  };
30396
30423
 
30397
30424
  export { Tooltip as default };
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"> */
30426
+
30398
30427
  //# sourceMappingURL=index.es.js.map