Skip to content

Commit 4e8ef04

Browse files
authored
Remove button swap JS (#1917)
1 parent 896a746 commit 4e8ef04

File tree

5 files changed

+12
-274
lines changed

5 files changed

+12
-274
lines changed

.changeset/red-mice-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudfour/patterns': major
3+
---
4+
5+
Remove button-swap component JS. Users of the component will need to implement their own functionality for this component.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/button-swap/button-swap.stories.mdx

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
import { Canvas, Meta, Story } from '@storybook/addon-docs';
22
import { makeTwigInclude } from '../../make-twig-include.js';
3-
import { useEffect } from '@storybook/client-api';
43
import buttonSwap from './button-swap.twig';
5-
import { initButtonSwap } from './button-swap.ts';
6-
// Helper function to initialize toggling button JS
7-
const buttonSwapStory = (args) => {
8-
useEffect(() => {
9-
const buttonSwapEls = [...document.querySelectorAll('.js-button-swap')].map(
10-
(buttonSwapEl) =>
11-
initButtonSwap(buttonSwapEl, {
12-
initialCallback: () => console.log('Subscribed!'),
13-
swappedCallback: () => console.log('Unsubscribed!'),
14-
})
15-
);
16-
return () => {
17-
for (const buttonSwapEl of buttonSwapEls) buttonSwapEl.destroy();
18-
};
19-
});
20-
return buttonSwap(args);
21-
};
224

235
<Meta
246
title="Components/Button Swap"
@@ -67,7 +49,7 @@ You have the ability to pass `initialCallback` and `swappedCallback` callback fu
6749
},
6850
}}
6951
>
70-
{(args) => buttonSwapStory(args)}
52+
{(args) => buttonSwap(args)}
7153
</Story>
7254
</Canvas>
7355

@@ -83,71 +65,7 @@ You have the ability to pass `initialCallback` and `swappedCallback` callback fu
8365

8466
## JavaScript
8567

86-
You'll need to use JavaScript to select the Button Swap component via the `js-button-swap` CSS class and initialize it using the `initButtonSwap` function.
87-
88-
### Syntax
89-
90-
```js
91-
initButtonSwap(buttonSwapEl, options);
92-
```
93-
94-
### Parameters
95-
96-
#### `buttonSwapEl`
97-
98-
The `.js-button-swap` Button Swap wrapper element.
99-
100-
#### `options` (optional)
101-
102-
An object which allows you to optionally pass `initialCallback` and `swappedCallback` functions to be called when each button is clicked.
103-
104-
```js
105-
options?: {
106-
initialCallback?: (event: Event) => void;
107-
swappedCallback?: (event: Event) => void;
108-
}
109-
```
110-
111-
### Return value
112-
113-
An object with a `destroy` function that removes event listeners.
114-
115-
```js
116-
{ destroy: () => void }
117-
```
118-
119-
### Examples
120-
121-
#### Single Button Swap component on the page
122-
123-
```js
124-
// Initialize
125-
const buttonSwapEl = initButtonSwap(document.querySelector('.js-button-swap'), {
126-
initialCallback: () => console.log('Subscribed!'),
127-
swappedCallback: () => console.log('Unsubscribed!'),
128-
});
129-
130-
// Remove all event listeners
131-
buttonSwapEl.destroy();
132-
```
133-
134-
#### Multiple Button Swap components on the page
135-
136-
```js
137-
// Initialize
138-
const buttonSwapEls = [...document.querySelectorAll('.js-button-swap')].map(
139-
(buttonSwapEl) =>
140-
initButtonSwap(buttonSwapEl, {
141-
initialCallback: () => console.log('Subscribed!'),
142-
swappedCallback: () => console.log('Unsubscribed!'),
143-
})
144-
);
145-
146-
// Remove all event listeners
147-
buttonSwapEls.forEach((buttonSwapEl) => {
148-
buttonSwapEl.destroy();
149-
});
150-
```
68+
This component does not come with built-in JavaScript for its functionality; you will need to write your own JS to use it.
15169

15270
## Unsupported features
15371

Lines changed: 3 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
22

3-
import type { ElementHandle, PleasantestUtils } from 'pleasantest';
3+
import type { ElementHandle } from 'pleasantest';
44
import { getAccessibilityTree, withBrowser } from 'pleasantest';
55

66
import { loadGlobalCSS, loadTwigTemplate } from '../../../test-utils.js';
@@ -10,28 +10,9 @@ const componentMarkup = loadTwigTemplate(
1010
path.join(__dirname, './button-swap.twig')
1111
);
1212

13-
// Helper to initialize the component JS
14-
const initJS = (
15-
utils: PleasantestUtils,
16-
initialCallback?: () => void,
17-
swappedCallback?: () => void
18-
) =>
19-
utils.runJS(
20-
`
21-
import { initButtonSwap } from './button-swap'
22-
export default (initialCallback, swappedCallback) => {
23-
initButtonSwap(document.querySelector('.js-button-swap'), {
24-
initialCallback,
25-
swappedCallback
26-
})
27-
}
28-
`,
29-
[initialCallback, swappedCallback]
30-
);
31-
3213
test(
3314
'Swap UI state when clicked',
34-
withBrowser(async ({ utils, screen, user, page }) => {
15+
withBrowser(async ({ utils, screen, page }) => {
3516
await utils.injectHTML(await componentMarkup());
3617
await loadGlobalCSS(utils);
3718

@@ -47,8 +28,6 @@ test(
4728
await expect(firstBtn).toBeVisible();
4829
await expect(firstBtn).not.toHaveClass('is-slashed');
4930

50-
await initJS(utils);
51-
5231
// After JS initializes it should be the same
5332
expect(await getAccessibilityTree(body)).toMatchInlineSnapshot(`
5433
status
@@ -59,41 +38,12 @@ test(
5938
firstBtn = await screen.queryByRole('button');
6039
await expect(firstBtn).toBeVisible();
6140
await expect(firstBtn).not.toHaveClass('is-slashed');
62-
63-
// Button swap action
64-
await user.click(firstBtn);
65-
66-
expect(await getAccessibilityTree(body)).toMatchInlineSnapshot(`
67-
alert (focused)
68-
text "Currently subscribed to notifications"
69-
button "Turn off notifications"
70-
`);
71-
72-
const secondBtn = await screen.queryByRole('button', {
73-
name: /^turn off notifications$/i,
74-
});
75-
await expect(secondBtn).toBeVisible();
76-
await expect(secondBtn).toHaveClass('is-slashed');
77-
78-
// Button swap action
79-
await user.click(secondBtn);
80-
81-
expect(await getAccessibilityTree(body)).toMatchInlineSnapshot(`
82-
alert (focused)
83-
text "Currently unsubscribed from notifications"
84-
button "Get notifications"
85-
`);
86-
87-
// Query for first button again in its new state
88-
firstBtn = await screen.queryByRole('button');
89-
await expect(firstBtn).toBeVisible();
90-
await expect(firstBtn).not.toHaveClass('is-slashed');
9141
})
9242
);
9343

9444
test(
9545
'Set custom messages and labels',
96-
withBrowser(async ({ utils, screen, user, page }) => {
46+
withBrowser(async ({ utils, page }) => {
9747
await utils.injectHTML(
9848
await componentMarkup({
9949
initial_visual_label: 'Hello world',
@@ -103,51 +53,12 @@ test(
10353
})
10454
);
10555
await loadGlobalCSS(utils);
106-
await initJS(utils);
10756

10857
const body = await page.evaluateHandle<ElementHandle>(() => document.body);
10958
expect(await getAccessibilityTree(body)).toMatchInlineSnapshot(`
11059
status
11160
text "Unsubscribed"
11261
button "Hello world"
11362
`);
114-
115-
// Button swap action
116-
await user.click(await screen.getByRole('button'));
117-
118-
expect(await getAccessibilityTree(body)).toMatchInlineSnapshot(`
119-
alert (focused)
120-
text "Subscribed"
121-
button "Have a great day"
122-
`);
123-
})
124-
);
125-
126-
test(
127-
'Callback functions are called',
128-
withBrowser(async ({ utils, screen, user }) => {
129-
await utils.injectHTML(await componentMarkup());
130-
await loadGlobalCSS(utils);
131-
132-
const mockInitialCallback = jest.fn();
133-
const mockSwappedCallback = jest.fn();
134-
135-
await initJS(utils, mockInitialCallback, mockSwappedCallback);
136-
137-
const firstBtn = await screen.queryByRole('button', {
138-
name: /^get notifications$/i,
139-
});
140-
141-
await user.click(firstBtn);
142-
143-
expect(mockInitialCallback).toBeCalledTimes(1);
144-
145-
const secondBtn = await screen.queryByRole('button', {
146-
name: /^turn off notifications$/i,
147-
});
148-
149-
await user.click(secondBtn);
150-
151-
expect(mockSwappedCallback).toBeCalledTimes(1);
15263
})
15364
);

src/components/button-swap/button-swap.ts

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)