@adobe-commerce/elsie 1.3.0-beta5 → 1.3.0-beta6
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/package.json
CHANGED
|
@@ -12,6 +12,7 @@ import { Modal as component, ModalProps } from './Modal';
|
|
|
12
12
|
import { useState } from 'preact/hooks';
|
|
13
13
|
import { Button } from '../Button';
|
|
14
14
|
import { expect, userEvent, within, waitFor } from '@storybook/test';
|
|
15
|
+
import { useText } from '@adobe-commerce/elsie/i18n';
|
|
15
16
|
|
|
16
17
|
const meta: Meta<ModalProps> = {
|
|
17
18
|
title: 'Components/Modal',
|
|
@@ -269,4 +270,65 @@ export const OverflowingTitle: Story = {
|
|
|
269
270
|
},
|
|
270
271
|
};
|
|
271
272
|
|
|
273
|
+
const LocalizedContent = () => {
|
|
274
|
+
const translations = useText({
|
|
275
|
+
label: 'Dropin.ExampleComponentName.item.label',
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
console.log(translations.label);
|
|
279
|
+
return <div>{translations.label}</div>;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* ```ts
|
|
284
|
+
* import { Modal } from '@/elsie/components/Modal';
|
|
285
|
+
* import { useText } from '@adobe-commerce/elsie/i18n';
|
|
286
|
+
*
|
|
287
|
+
* const label = useText(`Dropin.ExampleComponentName.item.label`).label;
|
|
288
|
+
*
|
|
289
|
+
* <Modal size="medium" title={<h3>Localized Modal</h3>}>
|
|
290
|
+
* <div>{label}</div>
|
|
291
|
+
* </Modal>
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
|
|
295
|
+
export const LocalizedModal: Story = {
|
|
296
|
+
args: {
|
|
297
|
+
size: 'medium',
|
|
298
|
+
children: <LocalizedContent />,
|
|
299
|
+
title: <h3>Localized Modal</h3>,
|
|
300
|
+
},
|
|
301
|
+
play: async ({ canvasElement }) => {
|
|
302
|
+
const canvas = within(canvasElement);
|
|
303
|
+
await userEvent.click(canvas.getByRole('button'));
|
|
304
|
+
|
|
305
|
+
const portalRoot = await waitFor(() => {
|
|
306
|
+
const root = document.querySelector('[data-portal-root]') as HTMLDivElement;
|
|
307
|
+
expect(root).toBeTruthy();
|
|
308
|
+
return root;
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
await expect(portalRoot).toBeVisible();
|
|
312
|
+
|
|
313
|
+
const modal = document.querySelector(
|
|
314
|
+
'.dropin-modal__body'
|
|
315
|
+
) as HTMLDivElement;
|
|
316
|
+
|
|
317
|
+
await expect(modal).toBeVisible();
|
|
318
|
+
|
|
319
|
+
expect(portalRoot.querySelector('h3')?.innerText).toBe('Localized Modal');
|
|
320
|
+
expect((portalRoot.querySelector('.dropin-modal__body') as HTMLElement)?.innerText).toContain('string');
|
|
321
|
+
|
|
322
|
+
const closeButton = document.querySelector(
|
|
323
|
+
'.dropin-modal__header-close-button'
|
|
324
|
+
) as HTMLButtonElement;
|
|
325
|
+
|
|
326
|
+
await userEvent.click(closeButton);
|
|
327
|
+
|
|
328
|
+
await expect(modal).not.toBeVisible();
|
|
329
|
+
|
|
330
|
+
await expect(canvas.getByText('Open Modal')).toBeVisible();
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
|
|
272
334
|
export default meta;
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
* accompanying it.
|
|
8
8
|
*******************************************************************/
|
|
9
9
|
|
|
10
|
-
import { ComponentChildren
|
|
11
|
-
import { FunctionComponent,
|
|
10
|
+
import { ComponentChildren } from 'preact';
|
|
11
|
+
import { FunctionComponent, useLayoutEffect, useRef } from 'preact/compat';
|
|
12
12
|
|
|
13
13
|
interface PortalProps {
|
|
14
14
|
children: ComponentChildren;
|
|
@@ -16,28 +16,34 @@ interface PortalProps {
|
|
|
16
16
|
|
|
17
17
|
export const Portal: FunctionComponent<PortalProps> = ({ children }) => {
|
|
18
18
|
const portalRoot = useRef<HTMLDivElement | null>(null);
|
|
19
|
+
const contentRef = useRef<HTMLDivElement | null>(null);
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
useLayoutEffect(() => {
|
|
21
22
|
// Create portal root if it doesn't exist
|
|
22
23
|
if (!portalRoot.current) {
|
|
23
24
|
portalRoot.current = document.createElement('div');
|
|
24
25
|
portalRoot.current.setAttribute('data-portal-root', '');
|
|
25
|
-
portalRoot.current.classList.add('dropin-design');
|
|
26
26
|
document.body.appendChild(portalRoot.current);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
//
|
|
30
|
-
|
|
29
|
+
// Move content to portal root
|
|
30
|
+
if (contentRef.current && portalRoot.current) {
|
|
31
|
+
portalRoot.current.appendChild(contentRef.current);
|
|
32
|
+
}
|
|
31
33
|
|
|
32
34
|
// Cleanup
|
|
33
35
|
return () => {
|
|
34
36
|
if (portalRoot.current) {
|
|
35
|
-
render(null, portalRoot.current);
|
|
36
37
|
portalRoot.current.remove();
|
|
37
38
|
portalRoot.current = null;
|
|
38
39
|
}
|
|
39
40
|
};
|
|
40
|
-
}, [
|
|
41
|
+
}, []);
|
|
41
42
|
|
|
42
|
-
|
|
43
|
+
// Return a div that contains the children
|
|
44
|
+
return (
|
|
45
|
+
<div ref={contentRef} className="dropin-design">
|
|
46
|
+
{children}
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
43
49
|
};
|