@cypress-design/react-tooltip 0.1.0 → 0.3.1

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.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#68](https://github.com/cypress-io/cypress-design/pull/68) [`8c577fb`](https://github.com/cypress-io/cypress-design/commit/8c577fb0c6a0411cf8218dfe78281834df3d6f13) Thanks [@elevatebart](https://github.com/elevatebart)! - feat: publish `.mjs` files for esm intead of `.js`
8
+
9
+ ## 0.3.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#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
14
+
15
+ ## 0.2.0
16
+
17
+ ### Minor Changes
18
+
19
+ - [#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
20
+ - add disabled prop to tooltip
21
+
22
+ * [#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
23
+
3
24
  ## 0.1.0
4
25
 
5
26
  ### Minor 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
@@ -32,6 +32,19 @@ export interface TooltipProps {
32
32
  * The content of the tooltip.
33
33
  */
34
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
35
48
  }
36
49
 
37
50
  const ROTATE_MAP = {
@@ -43,7 +56,17 @@ const ROTATE_MAP = {
43
56
 
44
57
  export const Tooltip: React.FC<
45
58
  TooltipProps & React.HTMLProps<HTMLDivElement>
46
- > = ({ 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
+ }) => {
47
70
  const arrowRef = React.useRef(null)
48
71
  const [open, setOpen] = React.useState(false)
49
72
 
@@ -73,20 +96,34 @@ export const Tooltip: React.FC<
73
96
  placement,
74
97
  open,
75
98
  onOpenChange: setOpen,
76
- 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
+ ],
77
109
  whileElementsMounted: autoUpdate,
78
110
  })
79
111
 
80
112
  const { getReferenceProps, getFloatingProps } = useInteractions([
81
113
  useHover(context, {
82
- handleClose: safePolygon(),
114
+ handleClose: interactive ? safePolygon() : undefined,
115
+ enabled: !disabled,
116
+ }),
117
+ useFocus(context, {
118
+ enabled: !disabled,
83
119
  }),
84
- useFocus(context),
85
120
  useRole(context, { role: 'tooltip' }),
86
121
  useDismiss(context),
87
122
  ])
88
123
 
89
- const placementSide = calculatedPlacement.split('-')[0]
124
+ const placementSide = calculatedPlacement.split(
125
+ '-'
126
+ )[0] as keyof typeof ROTATE_MAP
90
127
  const arrowRotate = ROTATE_MAP[placementSide]
91
128
 
92
129
  let arrowXRule = 'left',
@@ -103,6 +140,27 @@ export const Tooltip: React.FC<
103
140
  arrowYRule = 'top'
104
141
  }
105
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
+
106
164
  return (
107
165
  <>
108
166
  <div
@@ -114,7 +172,7 @@ export const Tooltip: React.FC<
114
172
  {children}
115
173
  </div>
116
174
  <FloatingPortal>
117
- {open && (
175
+ {open && !disabled && (
118
176
  <div
119
177
  {...getFloatingProps({
120
178
  ref: floating,
@@ -126,45 +184,44 @@ export const Tooltip: React.FC<
126
184
  },
127
185
  })}
128
186
  >
129
- <svg
130
- ref={arrowRef}
131
- viewBox="0 0 48 48"
132
- width="24"
133
- height="24"
134
- className={clsx('absolute', [
135
- color === 'dark' && 'stroke-gray-800 fill-gray-900',
136
- color === 'light' && 'stroke-gray-100 fill-white',
137
- ])}
138
- style={{
139
- transform: `rotate(${arrowRotate}deg)`,
140
- filter:
141
- placementSide === 'bottom' || color === 'dark'
142
- ? undefined
143
- : 'drop-shadow(0 1px 1px rgba(225, 227, 237, .8))',
144
- [arrowXRule]: `${arrowX ?? -6}px`,
145
- [arrowYRule]: `${arrowY ?? -6}px`,
146
- }}
147
- fill="none"
148
- >
149
- <rect x="0" y="0" width="48" height="4" strokeWidth="0" />
150
- <path
151
- d="M 0 3 C 12 3 18 18 24 18 C 30 18 36 3 48 3"
152
- stroke-width="2"
153
- />
154
- </svg>
155
187
  <div
156
- className={clsx(
157
- 'rounded shadow border p-8px text-16px leading-24px min-w-160px text-center',
158
- [
159
- color === 'dark' &&
160
- 'bg-gray-900 shadow-gray-800 border-gray-800',
161
- color === 'light' &&
162
- 'bg-white shadow-gray-100 border-gray-100',
163
- (!x || !y) && 'invisible',
164
- ]
165
- )}
188
+ className={clsx('rounded shadow border', [
189
+ colors.background,
190
+ colors.block,
191
+ (!x || !y) && 'invisible',
192
+ ])}
166
193
  >
167
- {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>
168
225
  </div>
169
226
  </div>
170
227
  )}
package/dist/Tooltip.d.ts CHANGED
@@ -16,6 +16,19 @@ export interface TooltipProps {
16
16
  * The content of the tooltip.
17
17
  */
18
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;
19
32
  }
20
33
  export declare const Tooltip: React.FC<TooltipProps & React.HTMLProps<HTMLDivElement>>;
21
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;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"}
@@ -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(" + 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] = (arrowX !== null && arrowX !== void 0 ? arrowX : -6) + "px",
30384
- _b[arrowYRule] = (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 };
30398
- //# sourceMappingURL=index.es.js.map
30425
+
30426
+ /* <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"> */
30427
+ //# sourceMappingURL=index.es.mjs.map