Skip to content

Commit 6f4e461

Browse files
authored
ntp: remove legacy menu (#1638)
* ntp: remove legacy menu * fixing tests
1 parent bba5ecc commit 6f4e461

File tree

17 files changed

+92
-406
lines changed

17 files changed

+92
-406
lines changed

special-pages/pages/new-tab/app/components/App.js

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { Fragment, h } from 'preact';
22
import cn from 'classnames';
33
import styles from './App.module.css';
4-
import { useCustomizerDrawerSettings, useCustomizerKind, usePlatformName } from '../settings.provider.js';
4+
import { useCustomizerDrawerSettings, usePlatformName } from '../settings.provider.js';
55
import { WidgetList } from '../widget-list/WidgetList.js';
66
import { useGlobalDropzone } from '../dropzone.js';
7-
import {
8-
CustomizerMenu,
9-
CustomizerButton,
10-
CustomizerMenuPositionedFixed,
11-
useContextMenu,
12-
} from '../customizer/components/CustomizerMenu.js';
7+
import { CustomizerButton, CustomizerMenuPositionedFixed, useContextMenu } from '../customizer/components/CustomizerMenu.js';
138
import { useDrawer, useDrawerControls } from './Drawer.js';
149
import { CustomizerDrawer } from '../customizer/components/CustomizerDrawer.js';
1510
import { BackgroundConsumer } from './BackgroundProvider.js';
@@ -28,8 +23,6 @@ export function App() {
2823
const platformName = usePlatformName();
2924
const customizerDrawer = useCustomizerDrawerSettings();
3025

31-
const customizerKind = useCustomizerKind();
32-
3326
useGlobalDropzone();
3427
useContextMenu();
3528

@@ -63,42 +56,37 @@ export function App() {
6356
</main>
6457
<div class={styles.themeContext} data-theme={main}>
6558
<CustomizerMenuPositionedFixed>
66-
{customizerKind === 'menu' && <CustomizerMenu />}
67-
{customizerKind === 'drawer' && (
68-
<CustomizerButton
69-
buttonId={buttonId}
70-
menuId={drawerId}
71-
toggleMenu={toggle}
72-
buttonRef={buttonRef}
73-
isOpen={isOpen}
74-
kind={'drawer'}
75-
/>
76-
)}
59+
<CustomizerButton
60+
buttonId={buttonId}
61+
menuId={drawerId}
62+
toggleMenu={toggle}
63+
buttonRef={buttonRef}
64+
isOpen={isOpen}
65+
kind={'drawer'}
66+
/>
7767
</CustomizerMenuPositionedFixed>
7868
</div>
79-
{customizerKind === 'drawer' && (
80-
<aside
81-
class={cn(styles.aside, styles.asideLayout, styles.asideScroller)}
82-
tabindex={tabIndex}
83-
aria-hidden={hidden}
84-
data-theme={browser}
85-
data-browser-panel
86-
ref={asideRef}
87-
>
88-
<div class={styles.asideContent}>
89-
<InlineErrorBoundary
90-
context={'Customizer Drawer'}
91-
fallback={(message) => (
92-
<div class={styles.paddedError}>
93-
<p>{message}</p>
94-
</div>
95-
)}
96-
>
97-
<CustomizerDrawer displayChildren={displayChildren} />
98-
</InlineErrorBoundary>
99-
</div>
100-
</aside>
101-
)}
69+
<aside
70+
class={cn(styles.aside, styles.asideLayout, styles.asideScroller)}
71+
tabindex={tabIndex}
72+
aria-hidden={hidden}
73+
data-theme={browser}
74+
data-browser-panel
75+
ref={asideRef}
76+
>
77+
<div class={styles.asideContent}>
78+
<InlineErrorBoundary
79+
context={'Customizer Drawer'}
80+
fallback={(message) => (
81+
<div class={styles.paddedError}>
82+
<p>{message}</p>
83+
</div>
84+
)}
85+
>
86+
<CustomizerDrawer displayChildren={displayChildren} />
87+
</InlineErrorBoundary>
88+
</div>
89+
</aside>
10290
</div>
10391
</Fragment>
10492
);

special-pages/pages/new-tab/app/customizer/components/Customizer.examples.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { h } from 'preact';
22
import { noop } from '../../utils.js';
33
import { CustomizerButton } from './CustomizerMenu.js';
4-
import { EmbeddedVisibilityMenu, VisibilityMenu } from './VisibilityMenu.js';
4+
import { EmbeddedVisibilityMenu } from './VisibilityMenu.js';
55
import { BackgroundSection } from './BackgroundSection.js';
66
import { ColorSelection } from './ColorSelection.js';
77
import { GradientSelection } from './GradientSelection.js';
@@ -68,27 +68,6 @@ export const customizerExamples = {
6868
<MaxContent>
6969
<CustomizerButton isOpen={true} kind="menu" />
7070
<br />
71-
<VisibilityMenu
72-
rows={[
73-
{
74-
id: 'favorites',
75-
title: 'Favorites',
76-
icon: 'star',
77-
toggle: noop('toggle favorites'),
78-
visibility: 'hidden',
79-
index: 0,
80-
},
81-
{
82-
id: 'privacyStats',
83-
title: 'Privacy Stats',
84-
icon: 'shield',
85-
toggle: noop('toggle favorites'),
86-
visibility: 'visible',
87-
index: 1,
88-
},
89-
]}
90-
/>
91-
<br />
9271
<div style="width: 206px; border: 1px dotted black">
9372
<EmbeddedVisibilityMenu
9473
rows={[

special-pages/pages/new-tab/app/customizer/components/Customizer.module.css

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
1-
.root {
2-
position: relative
3-
}
4-
51
.lowerRightFixed {
62
position: absolute;
73
bottom: 1rem;
84
right: 1rem;
95
}
106

11-
.dropdownMenu {
12-
display: none;
13-
position: absolute;
14-
right: 0;
15-
bottom: calc(100% + 10px);
16-
}
17-
18-
.show {
19-
display: block;
20-
}
21-
227
/** todo: is this a re-usable button, yet? */
238
.customizeButton {
249
backdrop-filter: blur(48px);
Lines changed: 7 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,15 @@
11
import { h } from 'preact';
2-
import { useEffect, useRef, useState, useCallback, useId } from 'preact/hooks';
2+
import { useEffect } from 'preact/hooks';
33
import styles from './Customizer.module.css';
44
import { CustomizeIcon } from '../../components/Icons.js';
5-
import cn from 'classnames';
65
import { useMessaging, useTypedTranslation } from '../../types.js';
7-
import { VisibilityMenu, VisibilityMenuPopover } from './VisibilityMenu.js';
86

97
/**
108
* @import { Widgets, WidgetConfigItem, WidgetVisibility, VisibilityMenuItem } from '../../../types/new-tab.js'
119
*/
1210

13-
/**
14-
* Represents the NTP customizer. For now it's just the ability to toggle sections.
15-
*/
16-
export function CustomizerMenu() {
17-
const { setIsOpen, buttonRef, dropdownRef, isOpen } = useDropdown();
18-
const [rowData, setRowData] = useState(/** @type {VisibilityRowData[]} */ ([]));
19-
20-
/**
21-
* Dispatch an event every time the customizer is opened - this
22-
* allows widgets to register themselves and provide titles/icons etc.
23-
*/
24-
const toggleMenu = useCallback(() => {
25-
if (isOpen) return setIsOpen(false);
26-
setRowData(getItems());
27-
setIsOpen(true);
28-
}, [isOpen]);
29-
30-
useEffect(() => {
31-
if (!isOpen) return;
32-
function handler() {
33-
setRowData(getItems());
34-
}
35-
window.addEventListener(CustomizerMenu.UPDATE_EVENT, handler);
36-
return () => {
37-
window.removeEventListener(CustomizerMenu.UPDATE_EVENT, handler);
38-
};
39-
}, [isOpen]);
40-
41-
const MENU_ID = useId();
42-
const BUTTON_ID = useId();
43-
44-
return (
45-
<div class={styles.root} ref={dropdownRef}>
46-
<CustomizerButton
47-
buttonId={BUTTON_ID}
48-
menuId={MENU_ID}
49-
toggleMenu={toggleMenu}
50-
buttonRef={buttonRef}
51-
isOpen={isOpen}
52-
kind={'menu'}
53-
/>
54-
<div id={MENU_ID} class={cn(styles.dropdownMenu, { [styles.show]: isOpen })} aria-labelledby={BUTTON_ID}>
55-
<VisibilityMenuPopover>
56-
<VisibilityMenu rows={rowData} />
57-
</VisibilityMenuPopover>
58-
</div>
59-
</div>
60-
);
61-
}
62-
63-
CustomizerMenu.OPEN_EVENT = 'ntp-customizer-open';
64-
CustomizerMenu.UPDATE_EVENT = 'ntp-customizer-update';
11+
export const OPEN_EVENT = 'ntp-customizer-open';
12+
export const UPDATE_EVENT = 'ntp-customizer-update';
6513

6614
export function getItems() {
6715
/** @type {VisibilityRowData[]} */
@@ -71,7 +19,7 @@ export function getItems() {
7119
next.push(incoming);
7220
},
7321
};
74-
const event = new CustomEvent(CustomizerMenu.OPEN_EVENT, { detail });
22+
const event = new CustomEvent(OPEN_EVENT, { detail });
7523
window.dispatchEvent(event);
7624
next.sort((a, b) => a.index - b.index);
7725
return next;
@@ -137,48 +85,6 @@ export function CustomizerMenuPositionedFixed({ children }) {
13785
return <div class={styles.lowerRightFixed}>{children}</div>;
13886
}
13987

140-
function useDropdown() {
141-
/** @type {import("preact").Ref<HTMLDivElement>} */
142-
const dropdownRef = useRef(null);
143-
/** @type {import("preact").Ref<HTMLButtonElement>} */
144-
const buttonRef = useRef(null);
145-
146-
const [isOpen, setIsOpen] = useState(false);
147-
148-
/**
149-
* Event handlers when it's open
150-
*/
151-
useEffect(() => {
152-
if (!isOpen) return;
153-
const handleFocusOutside = (event) => {
154-
if (dropdownRef.current && !dropdownRef.current.contains(event.target) && !buttonRef.current?.contains(event.target)) {
155-
setIsOpen(false);
156-
}
157-
};
158-
const handleClickOutside = (event) => {
159-
if (dropdownRef.current && !dropdownRef.current.contains?.(event.target)) {
160-
setIsOpen(false);
161-
}
162-
};
163-
const handleKeyDown = (event) => {
164-
if (event.key === 'Escape') {
165-
setIsOpen(false);
166-
buttonRef.current?.focus?.();
167-
}
168-
};
169-
document.addEventListener('mousedown', handleClickOutside);
170-
document.addEventListener('keydown', handleKeyDown);
171-
document.addEventListener('focusin', handleFocusOutside);
172-
return () => {
173-
document.removeEventListener('mousedown', handleClickOutside);
174-
document.removeEventListener('keydown', handleKeyDown);
175-
document.removeEventListener('focusin', handleFocusOutside);
176-
};
177-
}, [isOpen]);
178-
179-
return { dropdownRef, buttonRef, isOpen, setIsOpen };
180-
}
181-
18288
export class VisibilityRowData {
18389
/**
18490
* @param {object} params
@@ -208,11 +114,11 @@ export function useCustomizer({ title, id, icon, toggle, visibility, index }) {
208114
const handler = (/** @type {CustomEvent<any>} */ e) => {
209115
e.detail.register({ title, id, icon, toggle, visibility, index });
210116
};
211-
window.addEventListener(CustomizerMenu.OPEN_EVENT, handler);
212-
return () => window.removeEventListener(CustomizerMenu.OPEN_EVENT, handler);
117+
window.addEventListener(OPEN_EVENT, handler);
118+
return () => window.removeEventListener(OPEN_EVENT, handler);
213119
}, [title, id, icon, toggle, visibility, index]);
214120

215121
useEffect(() => {
216-
window.dispatchEvent(new Event(CustomizerMenu.UPDATE_EVENT));
122+
window.dispatchEvent(new Event(UPDATE_EVENT));
217123
}, [visibility]);
218124
}
Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { h } from 'preact';
22
import cn from 'classnames';
3-
import { useId, useContext } from 'preact/hooks';
3+
import { useContext } from 'preact/hooks';
44

55
import { DuckFoot, Shield } from '../../components/Icons.js';
66
import styles from './VisibilityMenu.module.css';
@@ -13,55 +13,6 @@ import { CustomizerThemesContext } from '../CustomizerProvider.js';
1313
* @import { VisibilityRowData } from './CustomizerMenu.js'
1414
*/
1515

16-
/**
17-
* When the button is pressed, we dispatch an event to allow widgets to provide
18-
* meta data like translated titles
19-
*
20-
* @param {object} props
21-
* @param {VisibilityRowData[]} props.rows
22-
*/
23-
export function VisibilityMenu({ rows }) {
24-
const MENU_ID = useId();
25-
26-
return (
27-
<ul className={cn(styles.list)}>
28-
{rows.map((row) => {
29-
return (
30-
<li key={row.id}>
31-
<label className={styles.menuItemLabel} htmlFor={MENU_ID + row.id}>
32-
<input
33-
type="checkbox"
34-
checked={row.visibility === 'visible'}
35-
onChange={() => row.toggle?.(row.id)}
36-
id={MENU_ID + row.id}
37-
class={styles.checkbox}
38-
/>
39-
<span aria-hidden={true} className={styles.checkboxIcon}>
40-
{row.visibility === 'visible' && (
41-
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
42-
<path
43-
d="M3.5 9L6 11.5L12.5 5"
44-
stroke="white"
45-
stroke-width="1.5"
46-
stroke-linecap="round"
47-
stroke-linejoin="round"
48-
/>
49-
</svg>
50-
)}
51-
</span>
52-
<span className={styles.svg}>
53-
{row.icon === 'shield' && <DuckFoot />}
54-
{row.icon === 'star' && <Shield />}
55-
</span>
56-
<span>{row.title ?? row.id}</span>
57-
</label>
58-
</li>
59-
);
60-
})}
61-
</ul>
62-
);
63-
}
64-
6516
/**
6617
* @param {object} props
6718
* @param {VisibilityRowData[]} props.rows
@@ -97,7 +48,3 @@ export function EmbeddedVisibilityMenu({ rows }) {
9748
</ul>
9849
);
9950
}
100-
101-
export function VisibilityMenuPopover({ children }) {
102-
return <div className={styles.dropdownInner}>{children}</div>;
103-
}

0 commit comments

Comments
 (0)