@chayns-components/core 5.5.24 → 5.5.25

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.
Files changed (25) hide show
  1. package/AGENTS.md +44 -0
  2. package/lib/cjs/components/copyable-content/CopyableContent.test.js +38 -4
  3. package/lib/cjs/components/copyable-content/CopyableContent.test.js.map +1 -1
  4. package/lib/cjs/components/truncation/Truncation.js +61 -149
  5. package/lib/cjs/components/truncation/Truncation.js.map +1 -1
  6. package/lib/cjs/components/truncation/Truncation.styles.js +3 -4
  7. package/lib/cjs/components/truncation/Truncation.styles.js.map +1 -1
  8. package/lib/cjs/components/truncation/Truncation.test.js +61 -0
  9. package/lib/cjs/components/truncation/Truncation.test.js.map +1 -0
  10. package/lib/esm/components/copyable-content/CopyableContent.test.js +38 -4
  11. package/lib/esm/components/copyable-content/CopyableContent.test.js.map +1 -1
  12. package/lib/esm/components/truncation/Truncation.js +61 -149
  13. package/lib/esm/components/truncation/Truncation.js.map +1 -1
  14. package/lib/esm/components/truncation/Truncation.styles.js +2 -3
  15. package/lib/esm/components/truncation/Truncation.styles.js.map +1 -1
  16. package/lib/esm/components/truncation/Truncation.test.js +58 -0
  17. package/lib/esm/components/truncation/Truncation.test.js.map +1 -0
  18. package/lib/types/components/truncation/Truncation.d.ts +1 -1
  19. package/lib/types/components/truncation/Truncation.styles.d.ts +1 -1
  20. package/package.json +2 -2
  21. package/lib/cjs/utils/truncation.js +0 -42
  22. package/lib/cjs/utils/truncation.js.map +0 -1
  23. package/lib/esm/utils/truncation.js +0 -35
  24. package/lib/esm/utils/truncation.js.map +0 -1
  25. package/lib/types/utils/truncation.d.ts +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Truncation.test.js","names":["_react","require","_react2","_interopRequireDefault","_vitest","_Truncation","e","__esModule","default","vi","mock","Textstring","TextstringProvider","children","ttsToITextString","fn","resizeObserverCallbacks","ResizeObserverMock","constructor","callback","push","disconnect","observe","unobserve","stubGlobal","triggerResize","height","forEach","contentRect","afterEach","length","describe","it","render","createElement","collapsedHeight","waitFor","expect","screen","getByRole","name","toBeInTheDocument","queryByRole","not"],"sources":["../../../../src/components/truncation/Truncation.test.tsx"],"sourcesContent":["import { render, screen, waitFor } from '@testing-library/react';\nimport React from 'react';\nimport { afterEach, describe, expect, it, vi } from 'vitest';\nimport Truncation from './Truncation';\n\nvi.mock('@chayns-components/textstring', () => ({\n Textstring: () => 'Mehr anzeigen',\n TextstringProvider: ({ children }: { children: React.ReactNode }) => children,\n ttsToITextString: vi.fn(),\n}));\n\ntype ResizeObserverCallback = (entries: ResizeObserverEntry[]) => void;\n\nconst resizeObserverCallbacks: ResizeObserverCallback[] = [];\n\nclass ResizeObserverMock {\n constructor(callback: ResizeObserverCallback) {\n resizeObserverCallbacks.push(callback);\n }\n\n disconnect() {}\n\n observe() {}\n\n unobserve() {}\n}\n\nvi.stubGlobal('ResizeObserver', ResizeObserverMock);\n\nconst triggerResize = (height: number) => {\n resizeObserverCallbacks.forEach((callback) => {\n callback([\n {\n contentRect: { height } as DOMRectReadOnly,\n } as ResizeObserverEntry,\n ]);\n });\n};\n\nafterEach(() => {\n resizeObserverCallbacks.length = 0;\n});\n\ndescribe('Truncation', () => {\n it('shows the more action when content exceeds the collapsed height', async () => {\n render(\n <Truncation collapsedHeight={100}>\n <div>Long content</div>\n </Truncation>,\n );\n\n triggerResize(240);\n\n await waitFor(() => {\n expect(screen.getByRole('button', { name: 'Mehr anzeigen' })).toBeInTheDocument();\n });\n });\n\n it('keeps short content expanded without a more action', async () => {\n render(\n <Truncation collapsedHeight={100}>\n <div>Short content</div>\n </Truncation>,\n );\n\n triggerResize(64);\n\n await waitFor(() => {\n expect(screen.queryByRole('button', { name: 'Mehr anzeigen' })).not.toBeInTheDocument();\n });\n });\n});\n"],"mappings":";;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,OAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,OAAA,GAAA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AAA0B,aAAA,cAAK,cAAiB,CACd,CAChB,CAAC;IAEDT,aAAa,CAAC,GAAG,CAAC;IAElB,MAAM,IAAAW,cAAO,EAAC,MAAM;MAChB,IAAAC,cAAM,EAACC,aAAM,CAACC,SAAS,CAAC,QAAQ,EAAE;QAAEC,IAAI,EAAE;MAAgB,CAAC,CAAC,CAAC,CAACC,iBAAiB,CAAC,CAAC;IACrF,CAAC,CAAC;EACN,CAAC,CAAC;EAEF,IAAAT,UAAE,EAAC,oDAAoD,EAAE,YAAY;IACjE,IAAAC,aAAM,eACF/B,OAAA,CAAAM,OAAA,CAAA0B,aAAA,CAAC7B,WAAA,CAAAG,OAAU;MAAC2B,eAAe,EAAE;IAAI,gBAC7BjC,OAAA,CAAAM,OAAA,CAAA0B,aAAA,cAAK,eAAkB,CACf,CAChB,CAAC;IAEDT,aAAa,CAAC,EAAE,CAAC;IAEjB,MAAM,IAAAW,cAAO,EAAC,MAAM;MAChB,IAAAC,cAAM,EAACC,aAAM,CAACI,WAAW,CAAC,QAAQ,EAAE;QAAEF,IAAI,EAAE;MAAgB,CAAC,CAAC,CAAC,CAACG,GAAG,CAACF,iBAAiB,CAAC,CAAC;IAC3F,CAAC,CAAC;EACN,CAAC,CAAC;AACN,CAAC,CAAC","ignoreList":[]}
@@ -3,6 +3,37 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
3
3
  import React from 'react';
4
4
  import { describe, expect, it, vi } from 'vitest';
5
5
  import CopyableContent from './CopyableContent';
6
+ vi.mock('@chayns-components/textstring', () => ({
7
+ Textstring: ({
8
+ textstring
9
+ }) => textstring.fallback,
10
+ TextstringProvider: ({
11
+ children
12
+ }) => children,
13
+ ttsToITextString: textstring => textstring,
14
+ useTextstringValue: ({
15
+ textstring
16
+ }) => textstring.fallback
17
+ }));
18
+ const resizeObserverCallbacks = [];
19
+ class ResizeObserverMock {
20
+ constructor(callback) {
21
+ resizeObserverCallbacks.push(callback);
22
+ }
23
+ disconnect() {}
24
+ observe() {}
25
+ unobserve() {}
26
+ }
27
+ vi.stubGlobal('ResizeObserver', ResizeObserverMock);
28
+ const triggerResize = height => {
29
+ resizeObserverCallbacks.forEach(callback => {
30
+ callback([{
31
+ contentRect: {
32
+ height
33
+ }
34
+ }]);
35
+ });
36
+ };
6
37
  vi.mock('../icon/Icon', () => ({
7
38
  default: ({
8
39
  icons
@@ -84,14 +115,17 @@ describe('CopyableContent', () => {
84
115
  expect(container.querySelector('[data-copy-action="false"]')).toBeInTheDocument();
85
116
  expect(container.querySelector('[data-calling-code-action="false"]')).toBeInTheDocument();
86
117
  });
87
- it('supports collapsed content', () => {
118
+ it('supports collapsed content', async () => {
88
119
  render(/*#__PURE__*/React.createElement(CopyableContent, {
89
120
  collapsedHeight: 100,
90
121
  content: Array(20).fill('Long content').join('\n\n')
91
122
  }));
92
- expect(screen.getByRole('button', {
93
- name: 'Mehr anzeigen'
94
- })).toBeInTheDocument();
123
+ triggerResize(240);
124
+ await waitFor(() => {
125
+ expect(screen.getByRole('button', {
126
+ name: 'Mehr'
127
+ })).toBeInTheDocument();
128
+ });
95
129
  });
96
130
  });
97
131
  //# sourceMappingURL=CopyableContent.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"CopyableContent.test.js","names":["fireEvent","render","screen","waitFor","React","describe","expect","it","vi","CopyableContent","mock","default","icons","createElement","join","children","shouldShowCallingCodeAction","shouldShowCopyAction","importOriginal","createDialog","fn","open","readBlob","blob","Promise","resolve","reject","reader","FileReader","onerror","error","onload","result","readAsText","content","getByRole","name","toBeInTheDocument","toHaveAttribute","document","querySelector","not","write","spyOn","navigator","clipboard","mockResolvedValue","click","toHaveBeenCalled","item","calls","getType","resolves","toBe","container","collapsedHeight","Array","fill"],"sources":["../../../../src/components/copyable-content/CopyableContent.test.tsx"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-unsafe-call */\nimport { fireEvent, render, screen, waitFor } from '@testing-library/react';\nimport React from 'react';\nimport { describe, expect, it, vi } from 'vitest';\nimport CopyableContent from './CopyableContent';\n\nvi.mock('../icon/Icon', () => ({\n default: ({ icons }: { icons: string[] }) => <span data-icons={icons.join(' ')} />,\n}));\n\nvi.mock('../sharing-context-menu/SharingContextMenu', () => ({\n default: ({\n children,\n shouldShowCallingCodeAction,\n shouldShowCopyAction,\n }: {\n children: React.ReactNode;\n shouldShowCallingCodeAction?: boolean;\n shouldShowCopyAction?: boolean;\n }) => (\n <div\n data-calling-code-action={shouldShowCallingCodeAction}\n data-copy-action={shouldShowCopyAction}\n >\n {children}\n </div>\n ),\n}));\n\nvi.mock('chayns-api', async (importOriginal) => ({\n ...(await importOriginal<typeof import('chayns-api')>()),\n createDialog: vi.fn(() => ({ open: vi.fn() })),\n}));\n\nconst readBlob = (blob: Blob) =>\n new Promise<string>((resolve, reject) => {\n const reader = new FileReader();\n reader.onerror = () => reject(reader.error);\n reader.onload = () => resolve(reader.result as string);\n reader.readAsText(blob);\n });\n\ndescribe('CopyableContent', () => {\n it('renders formatted markdown without active input HTML', () => {\n render(\n <CopyableContent\n content={\n '# Heading\\n\\n- Entry\\n\\n[Link](https://example.com)\\n<script onload=\"x\">bad</script>'\n }\n />,\n );\n\n expect(screen.getByRole('heading', { name: 'Heading' })).toBeInTheDocument();\n expect(screen.getByRole('link', { name: 'Link' })).toHaveAttribute(\n 'href',\n 'https://example.com',\n );\n expect(document.querySelector('script')).not.toBeInTheDocument();\n expect(document.querySelector('[onload]')).not.toBeInTheDocument();\n });\n\n it('copies content instead of custom children', async () => {\n const write = vi.spyOn(navigator.clipboard, 'write').mockResolvedValue();\n render(<CopyableContent content=\"source markdown\">Visible replacement</CopyableContent>);\n\n fireEvent.click(screen.getByRole('button', { name: 'Kopieren' }));\n\n await waitFor(() => expect(write).toHaveBeenCalled());\n const item = write.mock.calls[0][0][0] as unknown as ClipboardItem;\n await expect(readBlob(await item.getType('text/plain'))).resolves.toBe('source markdown');\n });\n\n it('shows a checkmark after a successful copy', async () => {\n vi.spyOn(navigator.clipboard, 'write').mockResolvedValue();\n const { container } = render(<CopyableContent content=\"source\" />);\n\n fireEvent.click(screen.getByRole('button', { name: 'Kopieren' }));\n\n await waitFor(() => {\n expect(container.querySelector('[data-icons=\"fa fa-check\"]')).toBeInTheDocument();\n });\n });\n\n it('offers sharing actions', () => {\n const { container } = render(<CopyableContent content=\"source\" />);\n\n expect(screen.getByRole('button', { name: 'Teilen' })).toBeInTheDocument();\n expect(container.querySelector('[data-copy-action=\"false\"]')).toBeInTheDocument();\n expect(container.querySelector('[data-calling-code-action=\"false\"]')).toBeInTheDocument();\n });\n\n it('supports collapsed content', () => {\n render(\n <CopyableContent\n collapsedHeight={100}\n content={Array(20).fill('Long content').join('\\n\\n')}\n />,\n );\n\n expect(screen.getByRole('button', { name: 'Mehr anzeigen' })).toBeInTheDocument();\n });\n});\n"],"mappings":"AAAA;AACA,SAASA,SAAS,EAAEC,MAAM,EAAEC,MAAM,EAAEC,OAAO,QAAQ,wBAAwB;AAC3E,OAAOC,KAAK,MAAM,OAAO;AACzB,SAASC,QAAQ,EAAEC,MAAM,EAAEC,EAAE,EAAEC,EAAE,QAAQ,QAAQ;AACjD,OAAOC,eAAe,MAAM,mBAAmB;AAE/CD,EAAE,CAACE,IAAI,CAAC,cAAc,EAAE,OAAO;EAC3BC,OAAO,EAAEA,CAAC;IAAEC;EAA2B,CAAC,kBAAKR,KAAA,CAAAS,aAAA;IAAM,cAAYD,KAAK,CAACE,IAAI,CAAC,GAAG;EAAE,CAAE;AACrF,CAAC,CAAC,CAAC;AAEHN,EAAE,CAACE,IAAI,CAAC,4CAA4C,EAAE,OAAO;EACzDC,OAAO,EAAEA,CAAC;IACNI,QAAQ;IACRC,2BAA2B;IAC3BC;EAKJ,CAAC,kBACGb,KAAA,CAAAS,aAAA;IACI,4BAA0BG,2BAA4B;IACtD,oBAAkBC;EAAqB,GAEtCF,QACA;AAEb,CAAC,CAAC,CAAC;AAEHP,EAAE,CAACE,IAAI,CAAC,YAAY,EAAE,MAAOQ,cAAc,KAAM;EA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AC,CAAC;IACrF,CAAC,CAAC;EACN,CAAC,CAAC;EAEF9B,EAAE,CAAC,wBAAwB,EAAE,MAAM;IAC/B,MAAM;MAAE+C;IAAU,CAAC,GAAGrD,MAAM,cAACG,KAAA,CAAAS,aAAA,CAACJ,eAAe;MAACyB,OAAO,EAAC;IAAQ,CAAE,CAAC,CAAC;IAElE5B,MAAM,CAACJ,MAAM,CAACiC,SAAS,CAAC,QAAQ,EAAE;MAAEC,IAAI,EAAE;IAAS,CAAC,CAAC,CAAC,CAACC,iBAAiB,CAAC,CAAC;IAC1E/B,MAAM,CAACgD,SAAS,CAACd,aAAa,CAAC,4BAA4B,CAAC,CAAC,CAACH,iBAAiB,CAAC,CAAC;IACjF/B,MAAM,CAACgD,SAAS,CAACd,aAAa,CAAC,oCAAoC,CAAC,CAAC,CAACH,iBAAiB,CAAC,CAAC;EAC7F,CAAC,CAAC;EAEF9B,EAAE,CAAC,4BAA4B,EAAE,MAAM;IACnCN,MAAM,cACFG,KAAA,CAAAS,aAAA,CAACJ,eAAe;MACZ8C,eAAe,EAAE,GAAI;MACrBrB,OAAO,EAAEsB,KAAK,CAAC,EAAE,CAAC,CAACC,IAAI,CAAC,cAAc,CAAC,CAAC3C,IAAI,CAAC,MAAM;IAAE,CACxD,CACL,CAAC;IAEDR,MAAM,CAACJ,MAAM,CAACiC,SAAS,CAAC,QAAQ,EAAE;MAAEC,IAAI,EAAE;IAAgB,CAAC,CAAC,CAAC,CAACC,iBAAiB,CAAC,CAAC;EACrF,CAAC,CAAC;AACN,CAAC,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"CopyableContent.test.js","names":["fireEvent","render","screen","waitFor","React","describe","expect","it","vi","CopyableContent","mock","Textstring","textstring","fallback","TextstringProvider","children","ttsToITextString","useTextstringValue","resizeObserverCallbacks","ResizeObserverMock","constructor","callback","push","disconnect","observe","unobserve","stubGlobal","triggerResize","height","forEach","contentRect","default","icons","createElement","join","shouldShowCallingCodeAction","shouldShowCopyAction","importOriginal","createDialog","fn","open","readBlob","blob","Promise","resolve","reject","reader","FileReader","onerror","error","onload","result","readAsText","content","getByRole","name","toBeInTheDocument","toHaveAttribute","document","querySelector","not","write","spyOn","navigator","clipboard","mockResolvedValue","click","toHaveBeenCalled","item","calls","getType","resolves","toBe","container","collapsedHeight","Array","fill"],"sources":["../../../../src/components/copyable-content/CopyableContent.test.tsx"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-unsafe-call */\nimport { fireEvent, render, screen, waitFor } from '@testing-library/react';\nimport React from 'react';\nimport { describe, expect, it, vi } from 'vitest';\nimport CopyableContent from './CopyableContent';\n\nvi.mock('@chayns-components/textstring', () => ({\n Textstring: ({ textstring }: { textstring: { fallback: string } }) => textstring.fallback,\n TextstringProvider: ({ children }: { children: React.ReactNode }) => children,\n ttsToITextString: (textstring: { fallback: string }) => textstring,\n useTextstringValue: ({ textstring }: { textstring: { fallback: string } }) =>\n textstring.fallback,\n}));\n\ntype ResizeObserverCallback = (entries: ResizeObserverEntry[]) => void;\n\nconst resizeObserverCallbacks: ResizeObserverCallback[] = [];\n\nclass ResizeObserverMock {\n constructor(callback: ResizeObserverCallback) {\n resizeObserverCallbacks.push(callback);\n }\n\n disconnect() {}\n\n observe() {}\n\n unobserve() {}\n}\n\nvi.stubGlobal('ResizeObserver', ResizeObserverMock);\n\nconst triggerResize = (height: number) => {\n resizeObserverCallbacks.forEach((callback) => {\n callback([\n {\n contentRect: { height } as DOMRectReadOnly,\n } as ResizeObserverEntry,\n ]);\n });\n};\n\nvi.mock('../icon/Icon', () => ({\n default: ({ icons }: { icons: string[] }) => <span data-icons={icons.join(' ')} />,\n}));\n\nvi.mock('../sharing-context-menu/SharingContextMenu', () => ({\n default: ({\n children,\n shouldShowCallingCodeAction,\n shouldShowCopyAction,\n }: {\n children: React.ReactNode;\n shouldShowCallingCodeAction?: boolean;\n shouldShowCopyAction?: boolean;\n }) => (\n <div\n data-calling-code-action={shouldShowCallingCodeAction}\n data-copy-action={shouldShowCopyAction}\n >\n {children}\n </div>\n ),\n}));\n\nvi.mock('chayns-api', async (importOriginal) => ({\n ...(await importOriginal<typeof import('chayns-api')>()),\n createDialog: vi.fn(() => ({ open: vi.fn() })),\n}));\n\nconst readBlob = (blob: Blob) =>\n new Promise<string>((resolve, reject) => {\n const reader = new FileReader();\n reader.onerror = () => reject(reader.error);\n reader.onload = () => resolve(reader.result as string);\n reader.readAsText(blob);\n });\n\ndescribe('CopyableContent', () => {\n it('renders formatted markdown without active input HTML', () => {\n render(\n <CopyableContent\n content={\n '# Heading\\n\\n- Entry\\n\\n[Link](https://example.com)\\n<script onload=\"x\">bad</script>'\n }\n />,\n );\n\n expect(screen.getByRole('heading', { name: 'Heading' })).toBeInTheDocument();\n expect(screen.getByRole('link', { name: 'Link' })).toHaveAttribute(\n 'href',\n 'https://example.com',\n );\n expect(document.querySelector('script')).not.toBeInTheDocument();\n expect(document.querySelector('[onload]')).not.toBeInTheDocument();\n });\n\n it('copies content instead of custom children', async () => {\n const write = vi.spyOn(navigator.clipboard, 'write').mockResolvedValue();\n render(<CopyableContent content=\"source markdown\">Visible replacement</CopyableContent>);\n\n fireEvent.click(screen.getByRole('button', { name: 'Kopieren' }));\n\n await waitFor(() => expect(write).toHaveBeenCalled());\n const item = write.mock.calls[0][0][0] as unknown as ClipboardItem;\n await expect(readBlob(await item.getType('text/plain'))).resolves.toBe('source markdown');\n });\n\n it('shows a checkmark after a successful copy', async () => {\n vi.spyOn(navigator.clipboard, 'write').mockResolvedValue();\n const { container } = render(<CopyableContent content=\"source\" />);\n\n fireEvent.click(screen.getByRole('button', { name: 'Kopieren' }));\n\n await waitFor(() => {\n expect(container.querySelector('[data-icons=\"fa fa-check\"]')).toBeInTheDocument();\n });\n });\n\n it('offers sharing actions', () => {\n const { container } = render(<CopyableContent content=\"source\" />);\n\n expect(screen.getByRole('button', { name: 'Teilen' })).toBeInTheDocument();\n expect(container.querySelector('[data-copy-action=\"false\"]')).toBeInTheDocument();\n expect(container.querySelector('[data-calling-code-action=\"false\"]')).toBeInTheDocument();\n });\n\n it('supports collapsed content', async () => {\n render(\n <CopyableContent\n collapsedHeight={100}\n content={Array(20).fill('Long content').join('\\n\\n')}\n />,\n );\n\n triggerResize(240);\n\n await waitFor(() => {\n expect(screen.getByRole('button', { name: 'Mehr' })).toBeInTheDocument();\n });\n });\n});\n"],"mappings":"AAAA;AACA,SAASA,SAAS,EAAEC,MAAM,EAAEC,MAAM,EAAEC,OAAO,QAAQ,wBAAwB;AAC3E,OAAOC,KAAK,MAAM,OAAO;AACzB,SAASC,QAAQ,EAAEC,MAAM,EAAEC,EAAE,EAAEC,EAAE,QAAQ,QAAQ;AACjD,OAAOC,eAAe,MAAM,mBAAmB;AAE/CD,EAAE,CAACE,IAAI,CAAC,+BAA+B,EAAE,OAAO;EAC5CC,UAAU,EAAEA,CAAC;IAAEC;EAAiD,CAAC,KAAKA,UAAU,CAACC,QAAQ;EACzFC,kBAAkB,EAAEA,CAAC;IAAEC;EAAwC,CAAC,KAAKA,QAAQ;EAC7EC,gBAAgB,EAAGJ,UAAgC,IAAKA,UAAU;EAClEK,kBAAkB,EAAEA,CAAC;IAAEL;EAAiD,CAAC,KACrEA,UAAU,CAACC;AACnB,CAAC,CAAC,CAAC;AAIH,MAAMK,uBAAiD,GAAG,EAAE;AAE5D,MAAMC,kBAAkB,CAAC;EACrBC,WAAWA,CAACC,QAAgC,EAAE;IAC1CH,uBAAuB,CAACI,IAAI,CAACD,QAAQ,CAAC;EAC1C;EAEAE,UAAUA,CAAA,EAAG,CAAC;EAEdC,OAAOA,CAAA,EAAG,CAAC;EAEXC,SAASA,CAAA,EAAG,CAAC;AACjB;AAEAjB,EAAE,CAACkB,UAAU,CAAC,gBAAgB,EAAEP,kBAAkB,CAAC;AAEnD,MAAMQ,aAAa,GAAIC,MAAc,IAAK;EACtCV,uBAAuB,CAACW,OAAO,CAAER,QAAQ,IAAK;IAC1CA,QAAQ,CAAC,CACL;MACIS,WAAW,EAAE;QAAEF;MAAO;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MACZiE,eAAe,EAAE,GAAI;MACrBrB,OAAO,EAAEsB,KAAK,CAAC,EAAE,CAAC,CAACC,IAAI,CAAC,cAAc,CAAC,CAAC1C,IAAI,CAAC,MAAM;IAAE,CACxD,CACL,CAAC;IAEDP,aAAa,CAAC,GAAG,CAAC;IAElB,MAAMxB,OAAO,CAAC,MAAM;MAChBG,MAAM,CAACJ,MAAM,CAACoD,SAAS,CAAC,QAAQ,EAAE;QAAEC,IAAI,EAAE;MAAO,CAAC,CAAC,CAAC,CAACC,iBAAiB,CAAC,CAAC;IAC5E,CAAC,CAAC;EACN,CAAC,CAAC;AACN,CAAC,CAAC","ignoreList":[]}
@@ -1,57 +1,69 @@
1
- import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
2
- import { ClampPosition } from '../../types/truncation';
3
- import { debounce } from '../../utils/debounce';
4
- import { truncateElement } from '../../utils/truncation';
5
- import { StyledMotionTruncationContent, StyledTruncation, StyledTruncationClamp, StyledTruncationClampFocusWrapper, StyledTruncationClampWrapper, StyledTruncationPseudoContent } from './Truncation.styles';
6
1
  import { Textstring, TextstringProvider, ttsToITextString } from '@chayns-components/textstring';
7
- import textStrings from '../../constants/textStrings';
2
+ import React, { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
8
3
  import { useKeyboardFocusHighlighting } from '../../hooks/useKeyboardFocusHighlighting';
4
+ import textStrings from '../../constants/textStrings';
5
+ import { ClampPosition } from '../../types/truncation';
6
+ import { StyledMotionTruncationContent, StyledTruncation, StyledTruncationClamp, StyledTruncationClampFocusWrapper, StyledTruncationClampWrapper, StyledTruncationContent } from './Truncation.styles';
9
7
  const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
10
8
  const Truncation = ({
11
9
  collapsedHeight = 150,
12
10
  clampPosition = ClampPosition.Right,
13
11
  isOpen,
14
- moreLabel,
15
12
  lessLabel,
13
+ moreLabel,
16
14
  onChange,
17
15
  children,
18
16
  shouldEnableKeyboardHighlighting
19
17
  }) => {
20
18
  const shouldShowKeyboardHighlighting = useKeyboardFocusHighlighting(shouldEnableKeyboardHighlighting);
21
- const [internalIsOpen, setInternalIsOpen] = useState(false);
22
- const [showClamp, setShowClamp] = useState(true);
23
- const [newCollapsedHeight, setNewCollapsedHeight] = useState(collapsedHeight);
24
- const [originalHeight, setOriginalHeight] = useState(0);
25
- const [shouldShowCollapsedElement, setShouldShowCollapsedElement] = useState(true);
26
- const [hasSizeChanged, setHasSizeChanged] = useState(false);
27
- const [initialRender, setInitialRender] = useState(true);
28
- const [shouldSkipChangeCheck, setShouldSkipChangeCheck] = useState(false);
29
- const [originalSmallHeight, setOriginalSmallHeight] = useState(0);
30
- const [originalBigHeight, setOriginalBigHeight] = useState(0);
31
- useEffect(() => {
32
- setInitialRender(false);
33
- }, []);
34
- const parentRef = useRef(null);
35
- const pseudoChildrenRef = useRef(null);
36
- const childrenRef = useRef(null);
37
- const originalChildrenRef = useRef(null);
38
- const hasCollapsed = useRef(false);
39
- const isAnimating = useRef(false);
40
- const hasSizeRecentlyChanged = useRef(false);
41
- const canResetSizeChanged = useRef(true);
19
+ const contentRef = useRef(null);
20
+ const pendingObservedHeight = useRef(0);
21
+ const [internalIsOpen, setInternalIsOpen] = useState(Boolean(isOpen));
22
+ const [contentHeight, setContentHeight] = useState(0);
42
23
  useEffect(() => {
43
24
  if (typeof isOpen === 'boolean') {
44
25
  setInternalIsOpen(isOpen);
45
- setShowClamp(!isOpen);
46
26
  }
47
27
  }, [isOpen]);
48
-
49
- // Changes the state of the truncation
28
+ useIsomorphicLayoutEffect(() => {
29
+ if (!contentRef.current) {
30
+ return () => {};
31
+ }
32
+ let frame;
33
+ const updateContentHeight = entries => {
34
+ pendingObservedHeight.current = Math.max(pendingObservedHeight.current, entries?.[0]?.contentRect.height ?? 0);
35
+ if (frame !== undefined) {
36
+ return;
37
+ }
38
+ frame = window.requestAnimationFrame(() => {
39
+ frame = undefined;
40
+ if (!contentRef.current) {
41
+ return;
42
+ }
43
+ setContentHeight(Math.max(pendingObservedHeight.current, contentRef.current.scrollHeight, contentRef.current.getBoundingClientRect().height));
44
+ pendingObservedHeight.current = 0;
45
+ });
46
+ };
47
+ const resizeObserver = new ResizeObserver(entries => updateContentHeight(entries));
48
+ const mutationObserver = new MutationObserver(() => updateContentHeight());
49
+ resizeObserver.observe(contentRef.current);
50
+ mutationObserver.observe(contentRef.current, {
51
+ characterData: true,
52
+ childList: true,
53
+ subtree: true
54
+ });
55
+ updateContentHeight();
56
+ return () => {
57
+ resizeObserver.disconnect();
58
+ mutationObserver.disconnect();
59
+ if (frame !== undefined) {
60
+ window.cancelAnimationFrame(frame);
61
+ }
62
+ };
63
+ }, [children]);
50
64
  const handleClampClick = useCallback(event => {
51
65
  setInternalIsOpen(current => {
52
- if (typeof onChange === 'function') {
53
- onChange(event, !current);
54
- }
66
+ onChange?.(event, !current);
55
67
  return !current;
56
68
  });
57
69
  }, [onChange]);
@@ -61,129 +73,29 @@ const Truncation = ({
61
73
  event.currentTarget.click();
62
74
  }
63
75
  }, []);
64
- useEffect(() => {
65
- if (children) {
66
- setShouldSkipChangeCheck(true);
67
- window.setTimeout(() => {
68
- setShouldSkipChangeCheck(false);
69
- }, 200);
70
- }
71
- }, [children]);
72
- const handleAnimationEnd = useCallback(() => {
73
- hasCollapsed.current = true;
74
- isAnimating.current = false;
75
- if (canResetSizeChanged.current) {
76
- setHasSizeChanged(false);
77
- canResetSizeChanged.current = false;
78
- }
79
- window.setTimeout(() => {
80
- hasSizeRecentlyChanged.current = false;
81
- }, 10);
82
- setShouldShowCollapsedElement(!internalIsOpen);
83
- window.setTimeout(() => {
84
- hasCollapsed.current = false;
85
- }, 30);
86
- }, [internalIsOpen]);
87
- useEffect(() => {
88
- if (!pseudoChildrenRef.current) {
89
- return;
90
- }
91
- setOriginalHeight(pseudoChildrenRef.current.offsetHeight);
92
- setOriginalBigHeight(pseudoChildrenRef.current.offsetHeight);
93
- truncateElement(pseudoChildrenRef.current, collapsedHeight);
94
- setNewCollapsedHeight(pseudoChildrenRef.current.offsetHeight);
95
- setOriginalSmallHeight(pseudoChildrenRef.current.offsetHeight);
96
- }, [collapsedHeight, pseudoChildrenRef, children]);
97
-
98
- // Checks if the clamp should be shown
99
- useEffect(() => {
100
- if (pseudoChildrenRef.current && (!hasSizeChanged || shouldSkipChangeCheck) && !initialRender) {
101
- setShowClamp(originalHeight > newCollapsedHeight);
102
- }
103
- }, [shouldSkipChangeCheck, collapsedHeight, hasSizeChanged, initialRender, newCollapsedHeight, originalHeight, children]);
104
- useEffect(() => {
105
- if (childrenRef.current && pseudoChildrenRef.current && originalChildrenRef.current) {
106
- while (childrenRef.current.firstChild) {
107
- childrenRef.current.removeChild(childrenRef.current.firstChild);
108
- }
109
- childrenRef.current.appendChild(shouldShowCollapsedElement && !internalIsOpen ? pseudoChildrenRef.current : originalChildrenRef.current);
110
- parentRef.current?.appendChild(shouldShowCollapsedElement && !internalIsOpen ? originalChildrenRef.current : pseudoChildrenRef.current);
111
- childrenRef.current.children[0].style.visibility = 'visible';
112
- }
113
- }, [children, internalIsOpen, shouldShowCollapsedElement]);
114
- useIsomorphicLayoutEffect(() => {
115
- if (originalChildrenRef.current) {
116
- const resizeObserver = new ResizeObserver(entries => {
117
- if (entries && entries[0]) {
118
- const observedHeight = entries[0].contentRect.height;
119
- setOriginalHeight(observedHeight < originalBigHeight ? originalBigHeight : observedHeight);
120
- if (!hasCollapsed.current && !isAnimating.current && !hasSizeRecentlyChanged.current) {
121
- void debounce(() => {
122
- canResetSizeChanged.current = true;
123
- }, 250)();
124
- setHasSizeChanged(true);
125
- hasSizeRecentlyChanged.current = true;
126
- }
127
- }
128
- });
129
- resizeObserver.observe(originalChildrenRef.current);
130
- return () => {
131
- resizeObserver.disconnect();
132
- };
133
- }
134
- return () => {};
135
- }, [originalBigHeight, children]);
136
- useIsomorphicLayoutEffect(() => {
137
- if (pseudoChildrenRef.current) {
138
- const resizeObserver = new ResizeObserver(entries => {
139
- if (entries && entries[0]) {
140
- const observedHeight = entries[0].contentRect.height;
141
- setNewCollapsedHeight(observedHeight < originalSmallHeight ? originalSmallHeight : observedHeight);
142
- if (!hasCollapsed.current && !isAnimating.current && !hasSizeRecentlyChanged.current) {
143
- void debounce(() => {
144
- canResetSizeChanged.current = true;
145
- }, 250)();
146
- setHasSizeChanged(true);
147
- hasSizeRecentlyChanged.current = true;
148
- }
149
- }
150
- });
151
- resizeObserver.observe(pseudoChildrenRef.current);
152
- return () => {
153
- resizeObserver.disconnect();
154
- };
155
- }
156
- return () => {};
157
- }, [originalSmallHeight, children]);
158
- const ts = textStrings.components.truncation;
159
76
  const internalMoreLabel = moreLabel ?? /*#__PURE__*/React.createElement(Textstring, {
160
- textstring: ttsToITextString(ts.more)
77
+ textstring: ttsToITextString(textStrings.components.truncation.more)
161
78
  });
162
79
  const internalLessLabel = lessLabel ?? /*#__PURE__*/React.createElement(Textstring, {
163
- textstring: ttsToITextString(ts.less)
80
+ textstring: ttsToITextString(textStrings.components.truncation.less)
164
81
  });
165
- return useMemo(() => /*#__PURE__*/React.createElement(StyledTruncation, {
166
- className: "beta-chayns-truncation",
167
- ref: parentRef
168
- }, /*#__PURE__*/React.createElement(StyledTruncationPseudoContent, {
169
- ref: pseudoChildrenRef
170
- }, children), /*#__PURE__*/React.createElement(StyledTruncationPseudoContent, {
171
- ref: originalChildrenRef
172
- }, children), /*#__PURE__*/React.createElement(StyledMotionTruncationContent, {
82
+ const hasOverflow = contentHeight > collapsedHeight;
83
+ const collapsedContentHeight = Math.min(contentHeight || collapsedHeight, collapsedHeight);
84
+ const targetHeight = internalIsOpen ? contentHeight || collapsedHeight : collapsedContentHeight;
85
+ return /*#__PURE__*/React.createElement(StyledTruncation, {
86
+ className: "beta-chayns-truncation"
87
+ }, /*#__PURE__*/React.createElement(StyledMotionTruncationContent, {
173
88
  animate: {
174
- height: internalIsOpen ? originalHeight : newCollapsedHeight
89
+ height: targetHeight
175
90
  },
176
91
  initial: false,
177
92
  transition: {
178
93
  type: 'tween',
179
- duration: hasSizeChanged ? 0 : 0.2
180
- },
181
- onAnimationComplete: handleAnimationEnd,
182
- onAnimationStart: () => {
183
- isAnimating.current = true;
184
- },
185
- ref: childrenRef
186
- }), showClamp && /*#__PURE__*/React.createElement(StyledTruncationClampWrapper, {
94
+ duration: 0.2
95
+ }
96
+ }, /*#__PURE__*/React.createElement(StyledTruncationContent, {
97
+ ref: contentRef
98
+ }, children)), hasOverflow && /*#__PURE__*/React.createElement(StyledTruncationClampWrapper, {
187
99
  $position: clampPosition
188
100
  }, /*#__PURE__*/React.createElement(TextstringProvider, {
189
101
  libraryName: "@chayns-components-core"
@@ -194,7 +106,7 @@ const Truncation = ({
194
106
  onKeyDown: handleClampKeyDown,
195
107
  role: "button",
196
108
  tabIndex: 0
197
- }, internalIsOpen ? internalLessLabel : internalMoreLabel))))), [children, clampPosition, handleAnimationEnd, handleClampClick, handleClampKeyDown, hasSizeChanged, internalIsOpen, lessLabel, moreLabel, newCollapsedHeight, originalHeight, shouldShowKeyboardHighlighting, showClamp]);
109
+ }, internalIsOpen ? internalLessLabel : internalMoreLabel)))));
198
110
  };
199
111
  export default Truncation;
200
112
  //# sourceMappingURL=Truncation.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Truncation.js","names":["React","useCallback","useEffect","useLayoutEffect","useMemo","useRef","useState","ClampPosition","debounce","truncateElement","StyledMotionTruncationContent","StyledTruncation","StyledTruncationClamp","StyledTruncationClampFocusWrapper","StyledTruncationClampWrapper","StyledTruncationPseudoContent","Textstring","TextstringProvider","ttsToITextString","textStrings","useKeyboardFocusHighlighting","useIsomorphicLayoutEffect","window","Truncation","collapsedHeight","clampPosition","Right","isOpen","moreLabel","lessLabel","onChange","children","shouldEnableKeyboardHighlighting","shouldShowKeyboardHighlighting","internalIsOpen","setInternalIsOpen","showClamp","setShowClamp","newCollapsedHeight","setNewCollapsedHeight","originalHeight","setOriginalHeight","shouldShowCollapsedElement","setShouldShowCollapsedElement","hasSizeChanged","setHasSizeChanged","initialRender","setInitialRender","shouldSkipChangeCheck","setShouldSkipChangeCheck","originalSmallHeight","setOriginalSmallHeight","originalBigHeight","setOriginalBigHeight","parentRef","pseudoChildrenRef","childrenRef","originalChildrenRef","hasCollapsed","isAnimating","hasSizeRecentlyChanged","canResetSizeChanged","handleClampClick","event","current","handleClampKeyDown","key","preventDefault","currentTarget","click","setTimeout","handleAnimationEnd","offsetHeight","firstChild","removeChild","appendChild","style","visibility","resizeObserver","ResizeObserver","entries","observedHeight","contentRect","height","observe","disconnect","ts","components","truncation","internalMoreLabel","createElement","textstring","more","internalLessLabel","less","className","ref","animate","initial","transition","type","duration","onAnimationComplete","onAnimationStart","$position","libraryName","$shouldShowKeyboardHighlighting","onClick","onKeyDown","role","tabIndex"],"sources":["../../../../src/components/truncation/Truncation.tsx"],"sourcesContent":["import React, {\n FC,\n KeyboardEventHandler,\n MouseEvent,\n MouseEventHandler,\n ReactElement,\n useCallback,\n useEffect,\n useLayoutEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport { ClampPosition } from '../../types/truncation';\nimport { debounce } from '../../utils/debounce';\nimport { truncateElement } from '../../utils/truncation';\nimport {\n StyledMotionTruncationContent,\n StyledTruncation,\n StyledTruncationClamp,\n StyledTruncationClampFocusWrapper,\n StyledTruncationClampWrapper,\n StyledTruncationPseudoContent,\n} from './Truncation.styles';\nimport { Textstring, TextstringProvider, ttsToITextString } from '@chayns-components/textstring';\nimport textStrings from '../../constants/textStrings';\nimport { useKeyboardFocusHighlighting } from '../../hooks/useKeyboardFocusHighlighting';\n\nexport type TruncationProps = {\n /**\n * The elements that should be expanding or collapsing.\n */\n children: ReactElement;\n /**\n * The position of the clamp.\n */\n clampPosition?: ClampPosition;\n /**\n * The height of the children Element in it`s collapsed state.\n */\n collapsedHeight?: number;\n /**\n * If set to true, the content is exposed.\n */\n isOpen?: boolean;\n /**\n * A text that should be displayed if the content is expanded.\n */\n lessLabel?: string;\n /**\n * A text that should be displayed if the content is collapsed.\n */\n moreLabel?: string;\n /**\n * Function to be executed when the component is expanding or collapsing.\n */\n onChange?: (event: MouseEvent<HTMLAnchorElement>, isOpen: boolean) => void;\n /**\n * Enables keyboard-only focus highlighting.\n */\n shouldEnableKeyboardHighlighting?: boolean;\n};\n\nconst useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;\n\nconst Truncation: FC<TruncationProps> = ({\n collapsedHeight = 150,\n clampPosition = ClampPosition.Right,\n isOpen,\n moreLabel,\n lessLabel,\n onChange,\n children,\n shouldEnableKeyboardHighlighting,\n}) => {\n const shouldShowKeyboardHighlighting = useKeyboardFocusHighlighting(\n shouldEnableKeyboardHighlighting,\n );\n\n const [internalIsOpen, setInternalIsOpen] = useState(false);\n const [showClamp, setShowClamp] = useState(true);\n const [newCollapsedHeight, setNewCollapsedHeight] = useState(collapsedHeight);\n const [originalHeight, setOriginalHeight] = useState(0);\n const [shouldShowCollapsedElement, setShouldShowCollapsedElement] = useState(true);\n const [hasSizeChanged, setHasSizeChanged] = useState(false);\n const [initialRender, setInitialRender] = useState(true);\n const [shouldSkipChangeCheck, setShouldSkipChangeCheck] = useState(false);\n\n const [originalSmallHeight, setOriginalSmallHeight] = useState(0);\n const [originalBigHeight, setOriginalBigHeight] = useState(0);\n\n useEffect(() => {\n setInitialRender(false);\n }, []);\n\n const parentRef = useRef<HTMLDivElement>(null);\n const pseudoChildrenRef = useRef<HTMLDivElement>(null);\n const childrenRef = useRef<HTMLDivElement>(null);\n const originalChildrenRef = useRef<HTMLDivElement>(null);\n const hasCollapsed = useRef(false);\n const isAnimating = useRef(false);\n const hasSizeRecentlyChanged = useRef(false);\n const canResetSizeChanged = useRef(true);\n\n useEffect(() => {\n if (typeof isOpen === 'boolean') {\n setInternalIsOpen(isOpen);\n setShowClamp(!isOpen);\n }\n }, [isOpen]);\n\n // Changes the state of the truncation\n const handleClampClick = useCallback<MouseEventHandler<HTMLAnchorElement>>(\n (event) => {\n setInternalIsOpen((current) => {\n if (typeof onChange === 'function') {\n onChange(event, !current);\n }\n\n return !current;\n });\n },\n [onChange],\n );\n\n const handleClampKeyDown = useCallback<KeyboardEventHandler<HTMLAnchorElement>>((event) => {\n if (event.key === 'Enter' || event.key === ' ') {\n event.preventDefault();\n event.currentTarget.click();\n }\n }, []);\n\n useEffect(() => {\n if (children) {\n setShouldSkipChangeCheck(true);\n\n window.setTimeout(() => {\n setShouldSkipChangeCheck(false);\n }, 200);\n }\n }, [children]);\n\n const handleAnimationEnd = useCallback(() => {\n hasCollapsed.current = true;\n isAnimating.current = false;\n\n if (canResetSizeChanged.current) {\n setHasSizeChanged(false);\n canResetSizeChanged.current = false;\n }\n\n window.setTimeout(() => {\n hasSizeRecentlyChanged.current = false;\n }, 10);\n\n setShouldShowCollapsedElement(!internalIsOpen);\n\n window.setTimeout(() => {\n hasCollapsed.current = false;\n }, 30);\n }, [internalIsOpen]);\n\n useEffect(() => {\n if (!pseudoChildrenRef.current) {\n return;\n }\n\n setOriginalHeight(pseudoChildrenRef.current.offsetHeight);\n setOriginalBigHeight(pseudoChildrenRef.current.offsetHeight);\n\n truncateElement(pseudoChildrenRef.current, collapsedHeight);\n\n setNewCollapsedHeight(pseudoChildrenRef.current.offsetHeight);\n setOriginalSmallHeight(pseudoChildrenRef.current.offsetHeight);\n }, [collapsedHeight, pseudoChildrenRef, children]);\n\n // Checks if the clamp should be shown\n useEffect(() => {\n if (\n pseudoChildrenRef.current &&\n (!hasSizeChanged || shouldSkipChangeCheck) &&\n !initialRender\n ) {\n setShowClamp(originalHeight > newCollapsedHeight);\n }\n }, [\n shouldSkipChangeCheck,\n collapsedHeight,\n hasSizeChanged,\n initialRender,\n newCollapsedHeight,\n originalHeight,\n children,\n ]);\n\n useEffect(() => {\n if (childrenRef.current && pseudoChildrenRef.current && originalChildrenRef.current) {\n while (childrenRef.current.firstChild) {\n childrenRef.current.removeChild(childrenRef.current.firstChild);\n }\n\n childrenRef.current.appendChild(\n shouldShowCollapsedElement && !internalIsOpen\n ? pseudoChildrenRef.current\n : originalChildrenRef.current,\n );\n\n parentRef.current?.appendChild(\n shouldShowCollapsedElement && !internalIsOpen\n ? originalChildrenRef.current\n : pseudoChildrenRef.current,\n );\n\n (childrenRef.current.children[0] as HTMLDivElement).style.visibility = 'visible';\n }\n }, [children, internalIsOpen, shouldShowCollapsedElement]);\n\n useIsomorphicLayoutEffect(() => {\n if (originalChildrenRef.current) {\n const resizeObserver = new ResizeObserver((entries) => {\n if (entries && entries[0]) {\n const observedHeight = entries[0].contentRect.height;\n\n setOriginalHeight(\n observedHeight < originalBigHeight ? originalBigHeight : observedHeight,\n );\n\n if (\n !hasCollapsed.current &&\n !isAnimating.current &&\n !hasSizeRecentlyChanged.current\n ) {\n void debounce(() => {\n canResetSizeChanged.current = true;\n }, 250)();\n\n setHasSizeChanged(true);\n hasSizeRecentlyChanged.current = true;\n }\n }\n });\n\n resizeObserver.observe(originalChildrenRef.current);\n\n return () => {\n resizeObserver.disconnect();\n };\n }\n\n return () => {};\n }, [originalBigHeight, children]);\n\n useIsomorphicLayoutEffect(() => {\n if (pseudoChildrenRef.current) {\n const resizeObserver = new ResizeObserver((entries) => {\n if (entries && entries[0]) {\n const observedHeight = entries[0].contentRect.height;\n\n setNewCollapsedHeight(\n observedHeight < originalSmallHeight ? originalSmallHeight : observedHeight,\n );\n\n if (\n !hasCollapsed.current &&\n !isAnimating.current &&\n !hasSizeRecentlyChanged.current\n ) {\n void debounce(() => {\n canResetSizeChanged.current = true;\n }, 250)();\n\n setHasSizeChanged(true);\n hasSizeRecentlyChanged.current = true;\n }\n }\n });\n\n resizeObserver.observe(pseudoChildrenRef.current);\n\n return () => {\n resizeObserver.disconnect();\n };\n }\n\n return () => {};\n }, [originalSmallHeight, children]);\n\n const ts = textStrings.components.truncation;\n\n const internalMoreLabel = moreLabel ?? <Textstring textstring={ttsToITextString(ts.more)} />;\n const internalLessLabel = lessLabel ?? <Textstring textstring={ttsToITextString(ts.less)} />;\n\n return useMemo(\n () => (\n <StyledTruncation className=\"beta-chayns-truncation\" ref={parentRef}>\n <StyledTruncationPseudoContent ref={pseudoChildrenRef}>\n {children}\n </StyledTruncationPseudoContent>\n <StyledTruncationPseudoContent ref={originalChildrenRef}>\n {children}\n </StyledTruncationPseudoContent>\n <StyledMotionTruncationContent\n animate={{ height: internalIsOpen ? originalHeight : newCollapsedHeight }}\n initial={false}\n transition={{ type: 'tween', duration: hasSizeChanged ? 0 : 0.2 }}\n onAnimationComplete={handleAnimationEnd}\n onAnimationStart={() => {\n isAnimating.current = true;\n }}\n ref={childrenRef}\n />\n {showClamp && (\n <StyledTruncationClampWrapper $position={clampPosition}>\n <TextstringProvider libraryName=\"@chayns-components-core\">\n <StyledTruncationClampFocusWrapper\n $shouldShowKeyboardHighlighting={shouldShowKeyboardHighlighting}\n >\n <StyledTruncationClamp\n onClick={handleClampClick}\n onKeyDown={handleClampKeyDown}\n role=\"button\"\n tabIndex={0}\n >\n {internalIsOpen ? internalLessLabel : internalMoreLabel}\n </StyledTruncationClamp>\n </StyledTruncationClampFocusWrapper>\n </TextstringProvider>\n </StyledTruncationClampWrapper>\n )}\n </StyledTruncation>\n ),\n [\n children,\n clampPosition,\n handleAnimationEnd,\n handleClampClick,\n handleClampKeyDown,\n hasSizeChanged,\n internalIsOpen,\n lessLabel,\n moreLabel,\n newCollapsedHeight,\n originalHeight,\n shouldShowKeyboardHighlighting,\n showClamp,\n ],\n );\n};\n\nexport default Truncation;\n"],"mappings":"AAAA,OAAOA,KAAK,IAMRC,WAAW,EACXC,SAAS,EACTC,eAAe,EACfC,OAAO,EACPC,MAAM,EACNC,QAAQ,QACL,OAAO;AACd,SAASC,aAAa,QAAQ,wBAAwB;AACtD,SAASC,QAAQ,QAAQ,sBAAsB;AAC/C,SAASC,eAAe,QAAQ,wBAAwB;AACxD,SACIC,6BAA6B,EAC7BC,gBAAgB,EAChBC,qBAAqB,EACrBC,iCAAiC,EACjCC,4BAA4B,EAC5BC,6BAA6B,QAC1B,qB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AAA,CAAChF,6BAA6B;IAC1BuF,OAAO,EAAE;MAAEd,MAAM,EAAEjD,cAAc,GAAGM,cAAc,GAAGF;IAAmB,CAAE;IAC1E4D,OAAO,EAAE,KAAM;IACfC,UAAU,EAAE;MAAEC,IAAI,EAAE,OAAO;MAAEC,QAAQ,EAAEzD,cAAc,GAAG,CAAC,GAAG;IAAI,CAAE;IAClE0D,mBAAmB,EAAE/B,kBAAmB;IACxCgC,gBAAgB,EAAEA,CAAA,KAAM;MACpB5C,WAAW,CAACK,OAAO,GAAG,IAAI;IAC9B,CAAE;IACFgC,GAAG,EAAExC;EAAY,CACpB,CAAC,EACDpB,SAAS,iBACNpC,KAAA,CAAA0F,aAAA,CAAC5E,4BAA4B;IAAC0F,SAAS,EAAE/E;EAAc,gBACnDzB,KAAA,CAAA0F,aAAA,CAACzE,kBAAkB;IAACwF,WAAW,EAAC;EAAyB,gBACrDzG,KAAA,CAAA0F,aAAA,CAAC7E,iCAAiC;IAC9B6F,+BAA+B,EAAEzE;EAA+B,gBAEhEjC,KAAA,CAAA0F,aAAA,CAAC9E,qBAAqB;IAClB+F,OAAO,EAAE7C,gBAAiB;IAC1B8C,SAAS,EAAE3C,kBAAmB;IAC9B4C,IAAI,EAAC,QAAQ;IACbC,QAAQ,EAAE;EAAE,GAEX5E,cAAc,GAAG2D,iBAAiB,GAAGJ,iBACnB,CACQ,CACnB,CACM,CAEpB,CACrB,EACD,CACI1D,QAAQ,EACRN,aAAa,EACb8C,kBAAkB,EAClBT,gBAAgB,EAChBG,kBAAkB,EAClBrB,cAAc,EACdV,cAAc,EACdL,SAAS,EACTD,SAAS,EACTU,kBAAkB,EAClBE,cAAc,EACdP,8BAA8B,EAC9BG,SAAS,CAEjB,CAAC;AACL,CAAC;AAED,eAAeb,UAAU","ignoreList":[]}
1
+ {"version":3,"file":"Truncation.js","names":["Textstring","TextstringProvider","ttsToITextString","React","useCallback","useEffect","useLayoutEffect","useRef","useState","useKeyboardFocusHighlighting","textStrings","ClampPosition","StyledMotionTruncationContent","StyledTruncation","StyledTruncationClamp","StyledTruncationClampFocusWrapper","StyledTruncationClampWrapper","StyledTruncationContent","useIsomorphicLayoutEffect","window","Truncation","collapsedHeight","clampPosition","Right","isOpen","lessLabel","moreLabel","onChange","children","shouldEnableKeyboardHighlighting","shouldShowKeyboardHighlighting","contentRef","pendingObservedHeight","internalIsOpen","setInternalIsOpen","Boolean","contentHeight","setContentHeight","current","frame","updateContentHeight","entries","Math","max","contentRect","height","undefined","requestAnimationFrame","scrollHeight","getBoundingClientRect","resizeObserver","ResizeObserver","mutationObserver","MutationObserver","observe","characterData","childList","subtree","disconnect","cancelAnimationFrame","handleClampClick","event","handleClampKeyDown","key","preventDefault","currentTarget","click","internalMoreLabel","createElement","textstring","components","truncation","more","internalLessLabel","less","hasOverflow","collapsedContentHeight","min","targetHeight","className","animate","initial","transition","type","duration","ref","$position","libraryName","$shouldShowKeyboardHighlighting","onClick","onKeyDown","role","tabIndex"],"sources":["../../../../src/components/truncation/Truncation.tsx"],"sourcesContent":["import { Textstring, TextstringProvider, ttsToITextString } from '@chayns-components/textstring';\nimport React, {\n FC,\n KeyboardEventHandler,\n MouseEvent,\n MouseEventHandler,\n ReactElement,\n useCallback,\n useEffect,\n useLayoutEffect,\n useRef,\n useState,\n} from 'react';\nimport { useKeyboardFocusHighlighting } from '../../hooks/useKeyboardFocusHighlighting';\nimport textStrings from '../../constants/textStrings';\nimport { ClampPosition } from '../../types/truncation';\nimport {\n StyledMotionTruncationContent,\n StyledTruncation,\n StyledTruncationClamp,\n StyledTruncationClampFocusWrapper,\n StyledTruncationClampWrapper,\n StyledTruncationContent,\n} from './Truncation.styles';\n\nexport type TruncationProps = {\n /**\n * The elements that should be expanding or collapsing.\n */\n children: ReactElement;\n /**\n * The position of the clamp.\n */\n clampPosition?: ClampPosition;\n /**\n * The height of the children element in its collapsed state.\n */\n collapsedHeight?: number;\n /**\n * If set to true, the content is exposed.\n */\n isOpen?: boolean;\n /**\n * A text that should be displayed if the content is expanded.\n */\n lessLabel?: string;\n /**\n * A text that should be displayed if the content is collapsed.\n */\n moreLabel?: string;\n /**\n * Function to be executed when the component is expanding or collapsing.\n */\n onChange?: (event: MouseEvent<HTMLAnchorElement>, isOpen: boolean) => void;\n /**\n * Enables keyboard-only focus highlighting.\n */\n shouldEnableKeyboardHighlighting?: boolean;\n};\n\nconst useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;\n\nconst Truncation: FC<TruncationProps> = ({\n collapsedHeight = 150,\n clampPosition = ClampPosition.Right,\n isOpen,\n lessLabel,\n moreLabel,\n onChange,\n children,\n shouldEnableKeyboardHighlighting,\n}) => {\n const shouldShowKeyboardHighlighting = useKeyboardFocusHighlighting(\n shouldEnableKeyboardHighlighting,\n );\n const contentRef = useRef<HTMLDivElement>(null);\n const pendingObservedHeight = useRef(0);\n const [internalIsOpen, setInternalIsOpen] = useState(Boolean(isOpen));\n const [contentHeight, setContentHeight] = useState(0);\n\n useEffect(() => {\n if (typeof isOpen === 'boolean') {\n setInternalIsOpen(isOpen);\n }\n }, [isOpen]);\n\n useIsomorphicLayoutEffect(() => {\n if (!contentRef.current) {\n return () => {};\n }\n\n let frame: number | undefined;\n const updateContentHeight = (entries?: ResizeObserverEntry[]) => {\n pendingObservedHeight.current = Math.max(\n pendingObservedHeight.current,\n entries?.[0]?.contentRect.height ?? 0,\n );\n\n if (frame !== undefined) {\n return;\n }\n\n frame = window.requestAnimationFrame(() => {\n frame = undefined;\n\n if (!contentRef.current) {\n return;\n }\n\n setContentHeight(\n Math.max(\n pendingObservedHeight.current,\n contentRef.current.scrollHeight,\n contentRef.current.getBoundingClientRect().height,\n ),\n );\n pendingObservedHeight.current = 0;\n });\n };\n\n const resizeObserver = new ResizeObserver((entries) => updateContentHeight(entries));\n const mutationObserver = new MutationObserver(() => updateContentHeight());\n\n resizeObserver.observe(contentRef.current);\n mutationObserver.observe(contentRef.current, {\n characterData: true,\n childList: true,\n subtree: true,\n });\n updateContentHeight();\n\n return () => {\n resizeObserver.disconnect();\n mutationObserver.disconnect();\n\n if (frame !== undefined) {\n window.cancelAnimationFrame(frame);\n }\n };\n }, [children]);\n\n const handleClampClick = useCallback<MouseEventHandler<HTMLAnchorElement>>(\n (event) => {\n setInternalIsOpen((current) => {\n onChange?.(event, !current);\n return !current;\n });\n },\n [onChange],\n );\n\n const handleClampKeyDown = useCallback<KeyboardEventHandler<HTMLAnchorElement>>((event) => {\n if (event.key === 'Enter' || event.key === ' ') {\n event.preventDefault();\n event.currentTarget.click();\n }\n }, []);\n\n const internalMoreLabel = moreLabel ?? (\n <Textstring textstring={ttsToITextString(textStrings.components.truncation.more)} />\n );\n const internalLessLabel = lessLabel ?? (\n <Textstring textstring={ttsToITextString(textStrings.components.truncation.less)} />\n );\n const hasOverflow = contentHeight > collapsedHeight;\n const collapsedContentHeight = Math.min(contentHeight || collapsedHeight, collapsedHeight);\n const targetHeight = internalIsOpen ? contentHeight || collapsedHeight : collapsedContentHeight;\n\n return (\n <StyledTruncation className=\"beta-chayns-truncation\">\n <StyledMotionTruncationContent\n animate={{ height: targetHeight }}\n initial={false}\n transition={{ type: 'tween', duration: 0.2 }}\n >\n <StyledTruncationContent ref={contentRef}>{children}</StyledTruncationContent>\n </StyledMotionTruncationContent>\n {hasOverflow && (\n <StyledTruncationClampWrapper $position={clampPosition}>\n <TextstringProvider libraryName=\"@chayns-components-core\">\n <StyledTruncationClampFocusWrapper\n $shouldShowKeyboardHighlighting={shouldShowKeyboardHighlighting}\n >\n <StyledTruncationClamp\n onClick={handleClampClick}\n onKeyDown={handleClampKeyDown}\n role=\"button\"\n tabIndex={0}\n >\n {internalIsOpen ? internalLessLabel : internalMoreLabel}\n </StyledTruncationClamp>\n </StyledTruncationClampFocusWrapper>\n </TextstringProvider>\n </StyledTruncationClampWrapper>\n )}\n </StyledTruncation>\n );\n};\n\nexport default Truncation;\n"],"mappings":"AAAA,SAASA,UAAU,EAAEC,kBAAkB,EAAEC,gBAAgB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CAAAiE,aAAA,CAACxD,6BAA6B;IAC1BoE,OAAO,EAAE;MAAEnC,MAAM,EAAEiC;IAAa,CAAE;IAClCG,OAAO,EAAE,KAAM;IACfC,UAAU,EAAE;MAAEC,IAAI,EAAE,OAAO;MAAEC,QAAQ,EAAE;IAAI;EAAE,gBAE7CjF,KAAA,CAAAiE,aAAA,CAACnD,uBAAuB;IAACoE,GAAG,EAAEtD;EAAW,GAAEH,QAAkC,CAClD,CAAC,EAC/B+C,WAAW,iBACRxE,KAAA,CAAAiE,aAAA,CAACpD,4BAA4B;IAACsE,SAAS,EAAEhE;EAAc,gBACnDnB,KAAA,CAAAiE,aAAA,CAACnE,kBAAkB;IAACsF,WAAW,EAAC;EAAyB,gBACrDpF,KAAA,CAAAiE,aAAA,CAACrD,iCAAiC;IAC9ByE,+BAA+B,EAAE1D;EAA+B,gBAEhE3B,KAAA,CAAAiE,aAAA,CAACtD,qBAAqB;IAClB2E,OAAO,EAAE7B,gBAAiB;IAC1B8B,SAAS,EAAE5B,kBAAmB;IAC9B6B,IAAI,EAAC,QAAQ;IACbC,QAAQ,EAAE;EAAE,GAEX3D,cAAc,GAAGwC,iBAAiB,GAAGN,iBACnB,CACQ,CACnB,CACM,CAEpB,CAAC;AAE3B,CAAC;AAED,eAAe/C,UAAU","ignoreList":[]}
@@ -12,9 +12,8 @@ export const StyledMotionTruncationContent = styled(motion.div)`
12
12
  overflow: hidden;
13
13
  position: relative;
14
14
  `;
15
- export const StyledTruncationPseudoContent = styled.div`
16
- visibility: hidden;
17
- position: absolute;
15
+ export const StyledTruncationContent = styled.div`
16
+ min-width: 0;
18
17
  width: 100%;
19
18
  `;
20
19
  export const StyledTruncationClampWrapper = styled.div`
@@ -1 +1 @@
1
- {"version":3,"file":"Truncation.styles.js","names":["motion","styled","css","ClampPosition","keyboardFocusHighlightingRingCss","StyledTruncation","div","StyledMotionTruncationContent","StyledTruncationPseudoContent","StyledTruncationClampWrapper","$position","Left","Middle","StyledTruncationClampFocusWrapper","$shouldShowKeyboardHighlighting","StyledTruncationClamp","a"],"sources":["../../../../src/components/truncation/Truncation.styles.ts"],"sourcesContent":["import { motion } from 'motion/react';\nimport styled, { css } from 'styled-components';\nimport { ClampPosition } from '../../types/truncation';\nimport type { WithTheme } from '../color-scheme-provider/ColorSchemeProvider';\nimport { keyboardFocusHighlightingRingCss } from '../../utils/keyboardFocusHighlighting.styles';\n\nexport const StyledTruncation = styled.div`\n position: relative;\n overflow: hidden;\n`;\n\n// Fix framer-motion bug\nexport const StyledMotionTruncationContent = styled(motion.div)`\n overflow: hidden;\n position: relative;\n`;\n\nexport const StyledTruncationPseudoContent = styled.div`\n visibility: hidden;\n position: absolute;\n width: 100%;\n`;\n\ntype StyledTruncationClampWrapperProps = WithTheme<{ $position: ClampPosition }>;\n\nexport const StyledTruncationClampWrapper = styled.div<StyledTruncationClampWrapperProps>`\n display: flex;\n\n ${({ $position }) => {\n switch ($position) {\n case ClampPosition.Left:\n return css`\n justify-content: left;\n `;\n case ClampPosition.Middle:\n return css`\n justify-content: center;\n `;\n default:\n return css`\n justify-content: right;\n `;\n }\n }}\n`;\n\ntype StyledTruncationClampProps = {\n $shouldShowKeyboardHighlighting: boolean;\n};\n\nexport const StyledTruncationClampFocusWrapper = styled.div<StyledTruncationClampProps>`\n display: inline-block;\n border-radius: 3px;\n\n ${({ $shouldShowKeyboardHighlighting }) =>\n $shouldShowKeyboardHighlighting &&\n css`\n &:focus-within {\n transition: none;\n ${keyboardFocusHighlightingRingCss};\n color: inherit;\n }\n `}\n`;\n\nexport const StyledTruncationClamp = styled.a`\n cursor: pointer;\n z-index: 2;\n display: inline-block;\n line-height: 1.2;\n`;\n"],"mappings":"AAAA,SAASA,MAAM,QAAQ,cAAc;AACrC,OAAOC,MAAM,IAAIC,GAAG,QAAQ,mBAAmB;AAC/C,SAASC,aAAa,QAAQ,wBAAwB;AAEtD,SAASC,gCAAgC,QAAQ,8CAA8C;AAE/F,OAAO,MAAMC,gBAAgB,GAAGJ,MAAM,CAACK,GAAG;AAC1C;AACA;AACA,CAAC;;AAED;AACA,OAAO,MAAMC,6BAA6B,GAAGN,MAAM,CAACD,MAAM,CAACM,GAAG,CAAC;AAC/D;AACA;AACA,CAAC;AAED,OAAO,MAAME,6BAA6B,GAAGP,MAAM,CAACK,GAAG;AACvD;AACA;AACA;AACA,CAAC;AAID,OAAO,MAAMG,4BAA4B,GAAGR,MAAM,CAACK,GAAsC;AACzF;AACA;AACA,MAAM,CAAC;EAAEI;AAAU,CAAC,KAAK;EACjB,QAAQA,SAAS;IACb,KAAKP,aAAa,CAACQ,IAAI;MACnB,OAAOT,GAAG;AAC1B;AACA,iBAAiB;IACL,KAAKC,aAAa,CAACS,MAAM;MACrB,OAAOV,GAAG;AAC1B;AACA,iBAAiB;IACL;MACI,OAAOA,GAAG;AAC1B;AACA,iBAAiB;EACT;AACJ,CAAC;AACL,CAAC;AAMD,OAAO,MAAMW,iCAAiC,GAAGZ,MAAM,CAACK,GAA+B;AACvF;AACA;AACA;AACA,MAAM,CAAC;EAAEQ;AAAgC,CAAC,KAClCA,+BAA+B,IAC/BZ,GAAG;AACX;AACA;AACA,kBAAkBE,gCAAgC;AAClD;AACA;AACA,SAAS;AACT,CAAC;AAED,OAAO,MAAMW,qBAAqB,GAAGd,MAAM,CAACe,CAAC;AAC7C;AACA;AACA;AACA;AACA,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"Truncation.styles.js","names":["motion","styled","css","ClampPosition","keyboardFocusHighlightingRingCss","StyledTruncation","div","StyledMotionTruncationContent","StyledTruncationContent","StyledTruncationClampWrapper","$position","Left","Middle","StyledTruncationClampFocusWrapper","$shouldShowKeyboardHighlighting","StyledTruncationClamp","a"],"sources":["../../../../src/components/truncation/Truncation.styles.ts"],"sourcesContent":["import { motion } from 'motion/react';\nimport styled, { css } from 'styled-components';\nimport { ClampPosition } from '../../types/truncation';\nimport type { WithTheme } from '../color-scheme-provider/ColorSchemeProvider';\nimport { keyboardFocusHighlightingRingCss } from '../../utils/keyboardFocusHighlighting.styles';\n\nexport const StyledTruncation = styled.div`\n position: relative;\n overflow: hidden;\n`;\n\n// Fix framer-motion bug\nexport const StyledMotionTruncationContent = styled(motion.div)`\n overflow: hidden;\n position: relative;\n`;\n\nexport const StyledTruncationContent = styled.div`\n min-width: 0;\n width: 100%;\n`;\n\ntype StyledTruncationClampWrapperProps = WithTheme<{ $position: ClampPosition }>;\n\nexport const StyledTruncationClampWrapper = styled.div<StyledTruncationClampWrapperProps>`\n display: flex;\n\n ${({ $position }) => {\n switch ($position) {\n case ClampPosition.Left:\n return css`\n justify-content: left;\n `;\n case ClampPosition.Middle:\n return css`\n justify-content: center;\n `;\n default:\n return css`\n justify-content: right;\n `;\n }\n }}\n`;\n\ntype StyledTruncationClampProps = {\n $shouldShowKeyboardHighlighting: boolean;\n};\n\nexport const StyledTruncationClampFocusWrapper = styled.div<StyledTruncationClampProps>`\n display: inline-block;\n border-radius: 3px;\n\n ${({ $shouldShowKeyboardHighlighting }) =>\n $shouldShowKeyboardHighlighting &&\n css`\n &:focus-within {\n transition: none;\n ${keyboardFocusHighlightingRingCss};\n color: inherit;\n }\n `}\n`;\n\nexport const StyledTruncationClamp = styled.a`\n cursor: pointer;\n z-index: 2;\n display: inline-block;\n line-height: 1.2;\n`;\n"],"mappings":"AAAA,SAASA,MAAM,QAAQ,cAAc;AACrC,OAAOC,MAAM,IAAIC,GAAG,QAAQ,mBAAmB;AAC/C,SAASC,aAAa,QAAQ,wBAAwB;AAEtD,SAASC,gCAAgC,QAAQ,8CAA8C;AAE/F,OAAO,MAAMC,gBAAgB,GAAGJ,MAAM,CAACK,GAAG;AAC1C;AACA;AACA,CAAC;;AAED;AACA,OAAO,MAAMC,6BAA6B,GAAGN,MAAM,CAACD,MAAM,CAACM,GAAG,CAAC;AAC/D;AACA;AACA,CAAC;AAED,OAAO,MAAME,uBAAuB,GAAGP,MAAM,CAACK,GAAG;AACjD;AACA;AACA,CAAC;AAID,OAAO,MAAMG,4BAA4B,GAAGR,MAAM,CAACK,GAAsC;AACzF;AACA;AACA,MAAM,CAAC;EAAEI;AAAU,CAAC,KAAK;EACjB,QAAQA,SAAS;IACb,KAAKP,aAAa,CAACQ,IAAI;MACnB,OAAOT,GAAG;AAC1B;AACA,iBAAiB;IACL,KAAKC,aAAa,CAACS,MAAM;MACrB,OAAOV,GAAG;AAC1B;AACA,iBAAiB;IACL;MACI,OAAOA,GAAG;AAC1B;AACA,iBAAiB;EACT;AACJ,CAAC;AACL,CAAC;AAMD,OAAO,MAAMW,iCAAiC,GAAGZ,MAAM,CAACK,GAA+B;AACvF;AACA;AACA;AACA,MAAM,CAAC;EAAEQ;AAAgC,CAAC,KAClCA,+BAA+B,IAC/BZ,GAAG;AACX;AACA;AACA,kBAAkBE,gCAAgC;AAClD;AACA;AACA,SAAS;AACT,CAAC;AAED,OAAO,MAAMW,qBAAqB,GAAGd,MAAM,CAACe,CAAC;AAC7C;AACA;AACA;AACA;AACA,CAAC","ignoreList":[]}
@@ -0,0 +1,58 @@
1
+ import { render, screen, waitFor } from '@testing-library/react';
2
+ import React from 'react';
3
+ import { afterEach, describe, expect, it, vi } from 'vitest';
4
+ import Truncation from './Truncation';
5
+ vi.mock('@chayns-components/textstring', () => ({
6
+ Textstring: () => 'Mehr anzeigen',
7
+ TextstringProvider: ({
8
+ children
9
+ }) => children,
10
+ ttsToITextString: vi.fn()
11
+ }));
12
+ const resizeObserverCallbacks = [];
13
+ class ResizeObserverMock {
14
+ constructor(callback) {
15
+ resizeObserverCallbacks.push(callback);
16
+ }
17
+ disconnect() {}
18
+ observe() {}
19
+ unobserve() {}
20
+ }
21
+ vi.stubGlobal('ResizeObserver', ResizeObserverMock);
22
+ const triggerResize = height => {
23
+ resizeObserverCallbacks.forEach(callback => {
24
+ callback([{
25
+ contentRect: {
26
+ height
27
+ }
28
+ }]);
29
+ });
30
+ };
31
+ afterEach(() => {
32
+ resizeObserverCallbacks.length = 0;
33
+ });
34
+ describe('Truncation', () => {
35
+ it('shows the more action when content exceeds the collapsed height', async () => {
36
+ render(/*#__PURE__*/React.createElement(Truncation, {
37
+ collapsedHeight: 100
38
+ }, /*#__PURE__*/React.createElement("div", null, "Long content")));
39
+ triggerResize(240);
40
+ await waitFor(() => {
41
+ expect(screen.getByRole('button', {
42
+ name: 'Mehr anzeigen'
43
+ })).toBeInTheDocument();
44
+ });
45
+ });
46
+ it('keeps short content expanded without a more action', async () => {
47
+ render(/*#__PURE__*/React.createElement(Truncation, {
48
+ collapsedHeight: 100
49
+ }, /*#__PURE__*/React.createElement("div", null, "Short content")));
50
+ triggerResize(64);
51
+ await waitFor(() => {
52
+ expect(screen.queryByRole('button', {
53
+ name: 'Mehr anzeigen'
54
+ })).not.toBeInTheDocument();
55
+ });
56
+ });
57
+ });
58
+ //# sourceMappingURL=Truncation.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Truncation.test.js","names":["render","screen","waitFor","React","afterEach","describe","expect","it","vi","Truncation","mock","Textstring","TextstringProvider","children","ttsToITextString","fn","resizeObserverCallbacks","ResizeObserverMock","constructor","callback","push","disconnect","observe","unobserve","stubGlobal","triggerResize","height","forEach","contentRect","length","createElement","collapsedHeight","getByRole","name","toBeInTheDocument","queryByRole","not"],"sources":["../../../../src/components/truncation/Truncation.test.tsx"],"sourcesContent":["import { render, screen, waitFor } from '@testing-library/react';\nimport React from 'react';\nimport { afterEach, describe, expect, it, vi } from 'vitest';\nimport Truncation from './Truncation';\n\nvi.mock('@chayns-components/textstring', () => ({\n Textstring: () => 'Mehr anzeigen',\n TextstringProvider: ({ children }: { children: React.ReactNode }) => children,\n ttsToITextString: vi.fn(),\n}));\n\ntype ResizeObserverCallback = (entries: ResizeObserverEntry[]) => void;\n\nconst resizeObserverCallbacks: ResizeObserverCallback[] = [];\n\nclass ResizeObserverMock {\n constructor(callback: ResizeObserverCallback) {\n resizeObserverCallbacks.push(callback);\n }\n\n disconnect() {}\n\n observe() {}\n\n unobserve() {}\n}\n\nvi.stubGlobal('ResizeObserver', ResizeObserverMock);\n\nconst triggerResize = (height: number) => {\n resizeObserverCallbacks.forEach((callback) => {\n callback([\n {\n contentRect: { height } as DOMRectReadOnly,\n } as ResizeObserverEntry,\n ]);\n });\n};\n\nafterEach(() => {\n resizeObserverCallbacks.length = 0;\n});\n\ndescribe('Truncation', () => {\n it('shows the more action when content exceeds the collapsed height', async () => {\n render(\n <Truncation collapsedHeight={100}>\n <div>Long content</div>\n </Truncation>,\n );\n\n triggerResize(240);\n\n await waitFor(() => {\n expect(screen.getByRole('button', { name: 'Mehr anzeigen' })).toBeInTheDocument();\n });\n });\n\n it('keeps short content expanded without a more action', async () => {\n render(\n <Truncation collapsedHeight={100}>\n <div>Short content</div>\n </Truncation>,\n );\n\n triggerResize(64);\n\n await waitFor(() => {\n expect(screen.queryByRole('button', { name: 'Mehr anzeigen' })).not.toBeInTheDocument();\n });\n });\n});\n"],"mappings":"AAAA,SAASA,MAAM,EAAEC,MAAM,EAAEC,OAAO,QAAQ,wBAAwB;AAChE,OAAOC,KAAK,MAAM,OAAO;AACzB,SAASC,SAAS,EAAEC,QAAQ,EAAEC,MAAM,EAAEC,EAAE,EAAEC,EAAE,QAAQ,QAAQ;AAC5D,OAAOC,UAAU,MAAM,cAAc;AAErCD,EAAE,CAACE,IAAI,CAAC,+BAA+B,EAAE,OAAO;EAC5CC,UAAU,EAAEA,CAAA,KAAM,eAAe;EACjCC,kBAAkB,EAAEA,CAAC;IAAEC;EAAwC,CAAC,KAAKA,QAAQ;EAC7EC,gBAAgB,EAAEN,EAAE,CAACO,EAAE,CAAC;AAC5B,CAAC,CAAC,CAAC;AAIH,MAAMC,uBAAiD,GAAG,EAAE;AAE5D,MAAMC,kBAAkB,CAAC;EACrBC,WAAWA,CAACC,QAAgC,EAAE;IAC1CH,uBAAuB,CAACI,IAAI,CAACD,QAAQ,CAAC;EAC1C;EAEAE,UAAUA,CAAA,EAAG,CAAC;EAEdC,OAAOA,CAAA,EAAG,CAAC;EAEXC,SAASA,CA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AAC;AACN,CAAC,CAAC","ignoreList":[]}
@@ -10,7 +10,7 @@ export type TruncationProps = {
10
10
  */
11
11
  clampPosition?: ClampPosition;
12
12
  /**
13
- * The height of the children Element in it`s collapsed state.
13
+ * The height of the children element in its collapsed state.
14
14
  */
15
15
  collapsedHeight?: number;
16
16
  /**
@@ -5,7 +5,7 @@ export declare const StyledMotionTruncationContent: import("styled-components/di
5
5
  }, never> & Partial<Pick<Omit<Omit<import("framer-motion").HTMLMotionProps<"div">, "ref"> & React.RefAttributes<HTMLDivElement>, "ref"> & {
6
6
  ref?: ((instance: HTMLDivElement | null) => void | React.DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES[keyof React.DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES]) | React.RefObject<HTMLDivElement> | null | undefined;
7
7
  }, never>>> & string & Omit<import("framer-motion").ForwardRefComponent<HTMLDivElement, import("framer-motion").HTMLMotionProps<"div">>, keyof React.Component<any, {}, any>>;
8
- export declare const StyledTruncationPseudoContent: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never> & Partial<Pick<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>>> & string;
8
+ export declare const StyledTruncationContent: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never> & Partial<Pick<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>>> & string;
9
9
  export declare const StyledTruncationClampWrapper: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("styled-components").FastOmit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "theme" | "$position"> & {
10
10
  $position: ClampPosition;
11
11
  } & {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chayns-components/core",
3
- "version": "5.5.24",
3
+ "version": "5.5.25",
4
4
  "description": "A set of beautiful React components for developing your own applications with chayns.",
5
5
  "sideEffects": false,
6
6
  "browserslist": [
@@ -93,5 +93,5 @@
93
93
  "publishConfig": {
94
94
  "access": "public"
95
95
  },
96
- "gitHead": "531efb0f39cc8de34d09e69c4e601c24bc3b6153"
96
+ "gitHead": "fb124c67d5545a50ad85006b0ef94fa44a967f48"
97
97
  }