@toptal/picasso-typography-overflow 100.1.0 → 100.1.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.
@@ -13,8 +13,6 @@ export interface Props extends BaseProps, TypographyProps {
13
13
  tooltipDelay?: DelayType;
14
14
  /** Do not show tooltips for shorten content. */
15
15
  disableTooltip?: boolean;
16
- /** Do not cover the text with the layer that stops Safari from adding its own tooltip. Set it when children contain links or other controls, because the layer takes their clicks. */
17
- disableTooltipGuard?: boolean;
18
16
  /** Where should the tooltip be positioned */
19
17
  placement?: PlacementType;
20
18
  }
@@ -1 +1 @@
1
- {"version":3,"file":"TypographyOverflow.d.ts","sourceRoot":"","sources":["../../../src/TypographyOverflow/TypographyOverflow.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAEtC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAIvD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AACjE,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAGvE,MAAM,WAAW,KAAM,SAAQ,SAAS,EAAE,eAAe;IACvD,+CAA+C;IAC/C,QAAQ,CAAC,EAAE,SAAS,CAAA;IACpB,gCAAgC;IAChC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,kHAAkH;IAClH,cAAc,CAAC,EAAE,SAAS,CAAA;IAC1B,gEAAgE;IAChE,YAAY,CAAC,EAAE,SAAS,CAAA;IACxB,gDAAgD;IAChD,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,sLAAsL;IACtL,mBAAmB,CAAC,EAAE,OAAO,CAAA;IAC7B,6CAA6C;IAC7C,SAAS,CAAC,EAAE,aAAa,CAAA;CAC1B;AAED,eAAO,MAAM,kBAAkB;YAAW,KAAK;;CA4H9C,CAAA;AAID,eAAe,kBAAkB,CAAA"}
1
+ {"version":3,"file":"TypographyOverflow.d.ts","sourceRoot":"","sources":["../../../src/TypographyOverflow/TypographyOverflow.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAEtC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAIvD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AACjE,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAKvE,MAAM,WAAW,KAAM,SAAQ,SAAS,EAAE,eAAe;IACvD,+CAA+C;IAC/C,QAAQ,CAAC,EAAE,SAAS,CAAA;IACpB,gCAAgC;IAChC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,kHAAkH;IAClH,cAAc,CAAC,EAAE,SAAS,CAAA;IAC1B,gEAAgE;IAChE,YAAY,CAAC,EAAE,SAAS,CAAA;IACxB,gDAAgD;IAChD,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,6CAA6C;IAC7C,SAAS,CAAC,EAAE,aAAa,CAAA;CAC1B;AAED,eAAO,MAAM,kBAAkB;YAAW,KAAK;;CAoI9C,CAAA;AAID,eAAe,kBAAkB,CAAA"}
@@ -12,16 +12,27 @@ var __rest = (this && this.__rest) || function (s, e) {
12
12
  import React, { useCallback, useState } from 'react';
13
13
  import { Tooltip } from '@toptal/picasso-tooltip';
14
14
  import { Typography } from '@toptal/picasso-typography';
15
- import { isOverflown } from '@toptal/picasso-utils';
15
+ import { isOverflown, useIsomorphicLayoutEffect } from '@toptal/picasso-utils';
16
16
  import { twMerge } from '@toptal/picasso-tailwind-merge';
17
+ import isSafari from './is-safari';
17
18
  export const TypographyOverflow = (props) => {
18
- const { children, lines = 1, tooltipContent, tooltipDelay, disableTooltip, disableTooltipGuard, className, onClick, onMouseEnter, style, placement = 'top' } = props, rest = __rest(props, ["children", "lines", "tooltipContent", "tooltipDelay", "disableTooltip", "disableTooltipGuard", "className", "onClick", "onMouseEnter", "style", "placement"]);
19
+ const { children, lines = 1, tooltipContent, tooltipDelay, disableTooltip, className, onClick, onMouseEnter, style, placement = 'top' } = props, rest = __rest(props, ["children", "lines", "tooltipContent", "tooltipDelay", "disableTooltip", "className", "onClick", "onMouseEnter", "style", "placement"]);
19
20
  const [isTooltipActive, setIsTooltipActive] = useState(false);
20
21
  const [isTooltipOpened, setIsTooltipOpened] = useState(false);
21
22
  const [isTooltipAnimating, setIsTooltipAnimating] = useState(false);
23
+ const [isSafariBrowser, setIsSafariBrowser] = useState(false);
22
24
  const isTooltipRendered = isTooltipActive || isTooltipAnimating;
23
25
  const isMultiline = lines > 1;
24
- const isGuarded = !disableTooltip && !disableTooltipGuard;
26
+ // Safari never adds its tooltip to clamped multiline text, and without
27
+ // our tooltip its own one is the only way to read the full text
28
+ const hasSafariTooltipFix = isSafariBrowser && !isMultiline && !disableTooltip;
29
+ // Decided after mount, so server-rendered HTML stays the same, and set only in
30
+ // Safari, so other browsers render once and keep their markup
31
+ useIsomorphicLayoutEffect(() => {
32
+ if (isSafari()) {
33
+ setIsSafariBrowser(true);
34
+ }
35
+ }, []);
25
36
  // We are paying a very high price when using dynamic JSS rules
26
37
  // for a component that is used on a very large scale.
27
38
  // It was causing a major UI freezes and unnecessary style recalculations,
@@ -64,12 +75,11 @@ export const TypographyOverflow = (props) => {
64
75
  }, []);
65
76
  const typography = (React.createElement(Typography, Object.assign({}, rest, { style: extendedStyle, noWrap: !isMultiline, className: twMerge('[-webkit-box-orient:vertical] overflow-hidden text-ellipsis pr-[0.9px]', isMultiline
66
77
  ? '[display:-webkit-box] break-words ![white-space:initial]'
67
- : 'block', isGuarded && 'relative', className), onClick: handleClick, onMouseEnter: handleMouseEnter }),
68
- children,
69
- isGuarded && (
70
- // Safari shows a native tooltip when the element under the pointer has
71
- // no title and its block ends in an ellipsis; this empty layer is that element
72
- React.createElement("span", { "aria-hidden": true, className: 'absolute inset-0' }))));
78
+ : 'block',
79
+ // Safari adds its own tooltip when a line of this block ends in the ellipsis.
80
+ // An empty block after the text moves the text into an anonymous box, so
81
+ // the line that ends in the ellipsis isn't this block's own anymore
82
+ hasSafariTooltipFix && "after:block after:content-['']", className), onClick: handleClick, onMouseEnter: handleMouseEnter }), children));
73
83
  const tooltip = (React.createElement(Tooltip, { open: isTooltipOpened, content: tooltipContent !== null && tooltipContent !== void 0 ? tooltipContent : children, placement: placement, delay: tooltipDelay, interactive: true, disableListeners: disableTooltip, onOpen: handleTooltipOpen, onClose: handleTooltipClose, onTransitionExiting: handleTransitionExiting, onTransitionExited: handleTransitionExited }, typography));
74
84
  return isTooltipRendered ? tooltip : typography;
75
85
  };
@@ -1 +1 @@
1
- {"version":3,"file":"TypographyOverflow.js","sourceRoot":"","sources":["../../../src/TypographyOverflow/TypographyOverflow.tsx"],"names":[],"mappings":";;;;;;;;;;;AACA,OAAO,KAAK,EAAE,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAEpD,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAA;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAGnD,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAA;AAmBxD,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,KAAY,EAAE,EAAE;IACjD,MAAM,EACJ,QAAQ,EACR,KAAK,GAAG,CAAC,EACT,cAAc,EACd,YAAY,EACZ,cAAc,EACd,mBAAmB,EACnB,SAAS,EACT,OAAO,EACP,YAAY,EACZ,KAAK,EACL,SAAS,GAAG,KAAK,KAEf,KAAK,EADJ,IAAI,UACL,KAAK,EAbH,8JAaL,CAAQ,CAAA;IAET,MAAM,CAAC,eAAe,EAAE,kBAAkB,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IAC7D,MAAM,CAAC,eAAe,EAAE,kBAAkB,CAAC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gB,EAAE,cAAc,EAChC,MAAM,EAAE,iBAAiB,EACzB,OAAO,EAAE,kBAAkB,EAC3B,mBAAmB,EAAE,uBAAuB,EAC5C,kBAAkB,EAAE,sBAAsB,IAEzC,UAAU,CACH,CACX,CAAA;IAED,OAAO,iBAAiB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU,CAAA;AACjD,CAAC,CAAA;AAED,kBAAkB,CAAC,WAAW,GAAG,oBAAoB,CAAA;AAErD,eAAe,kBAAkB,CAAA"}
@@ -0,0 +1,3 @@
1
+ declare const isSafari: () => boolean;
2
+ export default isSafari;
3
+ //# sourceMappingURL=is-safari.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-safari.d.ts","sourceRoot":"","sources":["../../../src/TypographyOverflow/is-safari.ts"],"names":[],"mappings":"AAOA,QAAA,MAAM,QAAQ,eASb,CAAA;AAED,eAAe,QAAQ,CAAA"}
@@ -0,0 +1,16 @@
1
+ import { isBrowser } from '@toptal/picasso-utils';
2
+ let isSafariBrowser;
3
+ // Checked once, on the first call, which comes from an effect, so the server
4
+ // never reads navigator. jsdom also reports Apple as the vendor, so the user
5
+ // agent has to name Safari too
6
+ const isSafari = () => {
7
+ if (isSafariBrowser === undefined) {
8
+ isSafariBrowser =
9
+ isBrowser() &&
10
+ navigator.vendor === 'Apple Computer, Inc.' &&
11
+ /Safari\//.test(navigator.userAgent);
12
+ }
13
+ return isSafariBrowser;
14
+ };
15
+ export default isSafari;
16
+ //# sourceMappingURL=is-safari.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-safari.js","sourceRoot":"","sources":["../../../src/TypographyOverflow/is-safari.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEjD,IAAI,eAAoC,CAAA;AAExC,6EAA6E;AAC7E,6EAA6E;AAC7E,+BAA+B;AAC/B,MAAM,QAAQ,GAAG,GAAG,EAAE;IACpB,IAAI,eAAe,KAAK,SAAS,EAAE,CAAC;QAClC,eAAe;YACb,SAAS,EAAE;gBACX,SAAS,CAAC,MAAM,KAAK,sBAAsB;gBAC3C,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;IACxC,CAAC;IAED,OAAO,eAAe,CAAA;AACxB,CAAC,CAAA;AAED,eAAe,QAAQ,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toptal/picasso-typography-overflow",
3
- "version": "100.1.0",
3
+ "version": "100.1.1",
4
4
  "description": "Toptal UI components library - TypographyOverflow",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -3,11 +3,13 @@ import React, { useCallback, useState } from 'react'
3
3
  import type { BaseProps } from '@toptal/picasso-shared'
4
4
  import { Tooltip } from '@toptal/picasso-tooltip'
5
5
  import { Typography } from '@toptal/picasso-typography'
6
- import { isOverflown } from '@toptal/picasso-utils'
6
+ import { isOverflown, useIsomorphicLayoutEffect } from '@toptal/picasso-utils'
7
7
  import type { TypographyProps } from '@toptal/picasso-typography'
8
8
  import type { DelayType, PlacementType } from '@toptal/picasso-tooltip'
9
9
  import { twMerge } from '@toptal/picasso-tailwind-merge'
10
10
 
11
+ import isSafari from './is-safari'
12
+
11
13
  export interface Props extends BaseProps, TypographyProps {
12
14
  /** A typography which can possibly overflow */
13
15
  children?: ReactNode
@@ -19,8 +21,6 @@ export interface Props extends BaseProps, TypographyProps {
19
21
  tooltipDelay?: DelayType
20
22
  /** Do not show tooltips for shorten content. */
21
23
  disableTooltip?: boolean
22
- /** Do not cover the text with the layer that stops Safari from adding its own tooltip. Set it when children contain links or other controls, because the layer takes their clicks. */
23
- disableTooltipGuard?: boolean
24
24
  /** Where should the tooltip be positioned */
25
25
  placement?: PlacementType
26
26
  }
@@ -32,7 +32,6 @@ export const TypographyOverflow = (props: Props) => {
32
32
  tooltipContent,
33
33
  tooltipDelay,
34
34
  disableTooltip,
35
- disableTooltipGuard,
36
35
  className,
37
36
  onClick,
38
37
  onMouseEnter,
@@ -44,9 +43,20 @@ export const TypographyOverflow = (props: Props) => {
44
43
  const [isTooltipActive, setIsTooltipActive] = useState(false)
45
44
  const [isTooltipOpened, setIsTooltipOpened] = useState(false)
46
45
  const [isTooltipAnimating, setIsTooltipAnimating] = useState(false)
46
+ const [isSafariBrowser, setIsSafariBrowser] = useState(false)
47
47
  const isTooltipRendered = isTooltipActive || isTooltipAnimating
48
48
  const isMultiline = lines > 1
49
- const isGuarded = !disableTooltip && !disableTooltipGuard
49
+ // Safari never adds its tooltip to clamped multiline text, and without
50
+ // our tooltip its own one is the only way to read the full text
51
+ const hasSafariTooltipFix = isSafariBrowser && !isMultiline && !disableTooltip
52
+
53
+ // Decided after mount, so server-rendered HTML stays the same, and set only in
54
+ // Safari, so other browsers render once and keep their markup
55
+ useIsomorphicLayoutEffect(() => {
56
+ if (isSafari()) {
57
+ setIsSafariBrowser(true)
58
+ }
59
+ }, [])
50
60
 
51
61
  // We are paying a very high price when using dynamic JSS rules
52
62
  // for a component that is used on a very large scale.
@@ -116,18 +126,16 @@ export const TypographyOverflow = (props: Props) => {
116
126
  isMultiline
117
127
  ? '[display:-webkit-box] break-words ![white-space:initial]'
118
128
  : 'block',
119
- isGuarded && 'relative',
129
+ // Safari adds its own tooltip when a line of this block ends in the ellipsis.
130
+ // An empty block after the text moves the text into an anonymous box, so
131
+ // the line that ends in the ellipsis isn't this block's own anymore
132
+ hasSafariTooltipFix && "after:block after:content-['']",
120
133
  className
121
134
  )}
122
135
  onClick={handleClick}
123
136
  onMouseEnter={handleMouseEnter}
124
137
  >
125
138
  {children}
126
- {isGuarded && (
127
- // Safari shows a native tooltip when the element under the pointer has
128
- // no title and its block ends in an ellipsis; this empty layer is that element
129
- <span aria-hidden className='absolute inset-0' />
130
- )}
131
139
  </Typography>
132
140
  )
133
141
 
@@ -0,0 +1,52 @@
1
+ const SAFARI_USER_AGENT =
2
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15'
3
+ const CHROME_USER_AGENT =
4
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'
5
+
6
+ const mockNavigator = (vendor: string, userAgent: string) => {
7
+ jest.spyOn(navigator, 'vendor', 'get').mockReturnValue(vendor)
8
+ jest.spyOn(navigator, 'userAgent', 'get').mockReturnValue(userAgent)
9
+ }
10
+
11
+ // The result is kept for the module's lifetime, so each test loads a fresh copy
12
+ const loadIsSafari = () => {
13
+ let isSafari = (): boolean => {
14
+ throw new Error('not loaded')
15
+ }
16
+
17
+ jest.isolateModules(() => {
18
+ isSafari = require('./is-safari').default
19
+ })
20
+
21
+ return isSafari
22
+ }
23
+
24
+ describe('isSafari', () => {
25
+ it('returns true for Safari', () => {
26
+ mockNavigator('Apple Computer, Inc.', SAFARI_USER_AGENT)
27
+
28
+ expect(loadIsSafari()()).toBe(true)
29
+ })
30
+
31
+ it('returns false for Chrome', () => {
32
+ mockNavigator('Google Inc.', CHROME_USER_AGENT)
33
+
34
+ expect(loadIsSafari()()).toBe(false)
35
+ })
36
+
37
+ it('returns false for jsdom, which also reports Apple as the vendor', () => {
38
+ expect(navigator.vendor).toBe('Apple Computer, Inc.')
39
+ expect(loadIsSafari()()).toBe(false)
40
+ })
41
+
42
+ it('checks the browser only once', () => {
43
+ mockNavigator('Apple Computer, Inc.', SAFARI_USER_AGENT)
44
+ const isSafari = loadIsSafari()
45
+
46
+ expect(isSafari()).toBe(true)
47
+
48
+ mockNavigator('Google Inc.', CHROME_USER_AGENT)
49
+
50
+ expect(isSafari()).toBe(true)
51
+ })
52
+ })
@@ -0,0 +1,19 @@
1
+ import { isBrowser } from '@toptal/picasso-utils'
2
+
3
+ let isSafariBrowser: boolean | undefined
4
+
5
+ // Checked once, on the first call, which comes from an effect, so the server
6
+ // never reads navigator. jsdom also reports Apple as the vendor, so the user
7
+ // agent has to name Safari too
8
+ const isSafari = () => {
9
+ if (isSafariBrowser === undefined) {
10
+ isSafariBrowser =
11
+ isBrowser() &&
12
+ navigator.vendor === 'Apple Computer, Inc.' &&
13
+ /Safari\//.test(navigator.userAgent)
14
+ }
15
+
16
+ return isSafariBrowser
17
+ }
18
+
19
+ export default isSafari
@@ -0,0 +1,36 @@
1
+ import React, { useState } from 'react'
2
+ import {
3
+ Button,
4
+ Container,
5
+ Link,
6
+ Typography,
7
+ TypographyOverflow,
8
+ } from '@toptal/picasso'
9
+
10
+ const Example = () => {
11
+ const [clicked, setClicked] = useState('nothing yet')
12
+
13
+ return (
14
+ <div style={{ width: 300, marginTop: 100 }}>
15
+ <TypographyOverflow data-testid='ellipsed-text'>
16
+ <Link href='#' onClick={() => setClicked('the link')}>
17
+ Invoice
18
+ </Link>{' '}
19
+ a client whose name is long enough to overflow
20
+ </TypographyOverflow>
21
+ <Container top='small'>
22
+ <TypographyOverflow>
23
+ <Button size='small' onClick={() => setClicked('the button')}>
24
+ Invoice
25
+ </Button>{' '}
26
+ a client whose name is long enough to overflow
27
+ </TypographyOverflow>
28
+ </Container>
29
+ <Container top='small'>
30
+ <Typography size='small'>Clicked: {clicked}</Typography>
31
+ </Container>
32
+ </div>
33
+ )
34
+ }
35
+
36
+ export default Example
@@ -15,35 +15,20 @@ const Example = () => {
15
15
  return (
16
16
  <>
17
17
  <DynamicWidthContainer style={{ marginTop: 150 }}>
18
- <TypographyOverflow
19
- lines={2}
20
- as='p'
21
- disableTooltipGuard
22
- data-testid='ellipsed-text-lines2'
23
- >
18
+ <TypographyOverflow lines={2} as='p' data-testid='ellipsed-text-lines2'>
24
19
  Two lines typography with a very long text and{' '}
25
20
  <a href='#'>two words link</a> and dynamic width. Try to resize.
26
21
  </TypographyOverflow>
27
22
  </DynamicWidthContainer>
28
23
  <DynamicWidthContainer>
29
- <TypographyOverflow
30
- lines={3}
31
- as='p'
32
- disableTooltipGuard
33
- data-testid='ellipsed-text-lines3'
34
- >
24
+ <TypographyOverflow lines={3} as='p' data-testid='ellipsed-text-lines3'>
35
25
  Three lines typography with a very long text and a very long text and{' '}
36
26
  a very long text and <a href='#'>two words link</a> and dynamic width.
37
27
  Try to resize.
38
28
  </TypographyOverflow>
39
29
  </DynamicWidthContainer>
40
30
  <DynamicWidthContainer>
41
- <TypographyOverflow
42
- lines={4}
43
- as='p'
44
- disableTooltipGuard
45
- data-testid='ellipsed-text-lines4'
46
- >
31
+ <TypographyOverflow lines={4} as='p' data-testid='ellipsed-text-lines4'>
47
32
  &lt;https://longlong.longlong.html/very/long/html/link/very/long/html/link/&gt;
48
33
  Four lines typography with a very long text and a very long text and a
49
34
  very long text and a very long text and a very long text and a very
@@ -32,6 +32,16 @@ page
32
32
  },
33
33
  'base/TypographyOverflow'
34
34
  )
35
+ .addExample(
36
+ 'TypographyOverflow/story/Link.example.tsx',
37
+ {
38
+ title: 'Link inside',
39
+ description:
40
+ 'Links and buttons inside stay clickable. The line below names the last one clicked.',
41
+ takeScreenshot: false,
42
+ },
43
+ 'base/TypographyOverflow'
44
+ )
35
45
  .addExample(
36
46
  'TypographyOverflow/story/CheckboxLabel.example.tsx',
37
47
  {
@@ -1,11 +1,13 @@
1
1
  import type { ReactNode } from 'react'
2
2
  import React from 'react'
3
- import { render, act } from '@toptal/picasso-test-utils'
3
+ import { renderToString } from 'react-dom/server'
4
+ import { render, act, screen, TestingPicasso } from '@toptal/picasso-test-utils'
4
5
  import { Tooltip } from '@toptal/picasso-tooltip'
5
6
  import { Typography } from '@toptal/picasso-typography'
6
7
  import { isOverflown } from '@toptal/picasso-utils'
7
8
 
8
9
  import { TypographyOverflow } from '.'
10
+ import isSafari from './is-safari'
9
11
 
10
12
  jest.mock('@toptal/picasso-tooltip', () => {
11
13
  return {
@@ -18,13 +20,16 @@ jest.mock('@toptal/picasso-tooltip', () => {
18
20
  jest.mock('@toptal/picasso-typography', () => {
19
21
  return {
20
22
  __esModule: true,
21
- Typography: jest.fn(({ children }: { children: ReactNode }) => (
22
- <span>{children}</span>
23
- )),
23
+ Typography: jest.fn(),
24
24
  }
25
25
  })
26
+ jest.mock('./is-safari', () => ({
27
+ __esModule: true,
28
+ default: jest.fn(() => false),
29
+ }))
26
30
  jest.mock('@toptal/picasso-utils', () => ({
27
31
  __esModule: true,
32
+ ...jest.requireActual('@toptal/picasso-utils'),
28
33
  isOverflown: jest.fn(() => true),
29
34
  }))
30
35
 
@@ -38,7 +43,24 @@ const mockedIsOverflown = isOverflown as jest.Mock<
38
43
  ReturnType<typeof isOverflown>
39
44
  >
40
45
 
46
+ const mockedIsSafari = isSafari as jest.Mock<ReturnType<typeof isSafari>>
47
+
48
+ const SAFARI_FIX_CLASSES = ['after:block', "after:content-['']"]
49
+
50
+ const mockSafari = () => {
51
+ mockedIsSafari.mockReturnValue(true)
52
+ }
53
+
54
+ const getRootClasses = () =>
55
+ mockedTypography.mock.calls.at(-1)?.[0].className.split(' ')
56
+
41
57
  describe('TypographyOverflow', () => {
58
+ beforeEach(() => {
59
+ mockedTypography.mockImplementation(
60
+ ({ children }: { children: ReactNode }) => <span>{children}</span>
61
+ )
62
+ })
63
+
42
64
  describe('initial render', () => {
43
65
  it('renders only typography', () => {
44
66
  render(
@@ -113,46 +135,89 @@ describe('TypographyOverflow', () => {
113
135
  })
114
136
  })
115
137
 
116
- describe('Safari tooltip guard', () => {
138
+ describe('title case', () => {
139
+ const { Typography: ActualTypography } = jest.requireActual(
140
+ '@toptal/picasso-typography'
141
+ )
142
+
117
143
  beforeEach(() => {
118
- mockedTypography.mockImplementation(({ children }) => (
119
- <span>{children}</span>
120
- ))
144
+ mockedTypography.mockImplementation(props =>
145
+ ActualTypography.render(props, null)
146
+ )
147
+ })
148
+
149
+ it('applies titleCase', () => {
150
+ render(<TypographyOverflow titleCase>invoice for</TypographyOverflow>)
151
+
152
+ expect(screen.getByText('Invoice For')).toBeInTheDocument()
121
153
  })
122
154
 
123
- const getRootClasses = () =>
124
- mockedTypography.mock.calls[0][0].className.split(' ')
155
+ it('applies titleCase in Safari', () => {
156
+ mockSafari()
125
157
 
126
- it('covers the text with a positioned layer by default', () => {
127
- const { container } = render(
128
- <TypographyOverflow>Just Typography</TypographyOverflow>
158
+ render(<TypographyOverflow titleCase>invoice for</TypographyOverflow>)
159
+
160
+ expect(getRootClasses()).toEqual(
161
+ expect.arrayContaining(SAFARI_FIX_CLASSES)
129
162
  )
163
+ expect(screen.getByText('Invoice For')).toBeInTheDocument()
164
+ })
165
+
166
+ it('keeps the text as written without titleCase', () => {
167
+ render(<TypographyOverflow>invoice for</TypographyOverflow>)
168
+
169
+ expect(screen.getByText('invoice for')).toBeInTheDocument()
170
+ })
171
+ })
172
+
173
+ describe('Safari tooltip fix', () => {
174
+ it('renders once and without the fix outside Safari', () => {
175
+ render(<TypographyOverflow>Just Typography</TypographyOverflow>)
130
176
 
131
- expect(container.querySelector('[aria-hidden="true"]')).toHaveClass(
132
- 'absolute',
133
- 'inset-0'
177
+ expect(mockedTypography).toHaveBeenCalledTimes(1)
178
+ expect(mockedTypography.mock.calls[0][0].className).not.toContain(
179
+ 'after:'
134
180
  )
135
- expect(getRootClasses()).toContain('relative')
136
181
  })
137
182
 
138
- it('leaves the text uncovered when disableTooltipGuard is set', () => {
139
- const { container } = render(
140
- <TypographyOverflow disableTooltipGuard>
141
- Just Typography
142
- </TypographyOverflow>
183
+ it('adds an empty block after the text in Safari', () => {
184
+ mockSafari()
185
+
186
+ render(<TypographyOverflow>Just Typography</TypographyOverflow>)
187
+
188
+ expect(getRootClasses()).toEqual(
189
+ expect.arrayContaining(SAFARI_FIX_CLASSES)
143
190
  )
191
+ })
144
192
 
145
- expect(container.querySelector('[aria-hidden="true"]')).toBeNull()
146
- expect(getRootClasses()).not.toContain('relative')
193
+ it('skips the fix for multiline text', () => {
194
+ mockSafari()
195
+
196
+ render(<TypographyOverflow lines={2}>Just Typography</TypographyOverflow>)
197
+
198
+ expect(getRootClasses()).not.toContain('after:block')
147
199
  })
148
200
 
149
- it('leaves the text uncovered when the tooltip is disabled', () => {
150
- const { container } = render(
201
+ it('skips the fix when the tooltip is disabled', () => {
202
+ mockSafari()
203
+
204
+ render(
151
205
  <TypographyOverflow disableTooltip>Just Typography</TypographyOverflow>
152
206
  )
153
207
 
154
- expect(container.querySelector('[aria-hidden="true"]')).toBeNull()
155
- expect(getRootClasses()).not.toContain('relative')
208
+ expect(getRootClasses()).not.toContain('after:block')
209
+ })
210
+
211
+ it('renders server HTML without the fix', () => {
212
+ mockSafari()
213
+
214
+ renderToString(
215
+ <TestingPicasso>
216
+ <TypographyOverflow>Just Typography</TypographyOverflow>
217
+ </TestingPicasso>
218
+ )
219
+
220
+ expect(getRootClasses()).not.toContain('after:block')
156
221
  })
157
222
  })
158
223