Skip to content

Commit

Permalink
Do not also handle right clicks
Browse files Browse the repository at this point in the history
  • Loading branch information
greyivy committed Jan 30, 2025
1 parent ff786ee commit a46c78a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { memo } from 'preact/compat';
import { ActivityApiContext, ActivityContext, ActivityProvider, SignalStateContext, SignalStateProvider } from '../ActivityProvider.js';
import { useTypedTranslationWith } from '../../types.js';
import { useVisibility } from '../../widget-list/widget-config.provider.js';
import { useAuxClick, useDocumentVisibility } from '../../utils.js';
import { useOnMiddleClick, useDocumentVisibility } from '../../utils.js';
import { useCustomizer } from '../../customizer/components/CustomizerMenu.js';
import { usePlatformName } from '../../settings.provider.js';
import { ActivityHeading } from '../../privacy-stats/components/PrivacyStats.js';
Expand Down Expand Up @@ -78,9 +78,8 @@ function ActivityConfigured({ expansion, toggle }) {
function ActivityBody({ canBurn }) {
const { didClick } = useContext(ActivityApiContext);

// handle middle clicks (preact doesn't seem to support `onAuxClick` out of the box)
const auxClickRef = useRef(/** @type {HTMLUListElement|null} */ (null));
useAuxClick(auxClickRef, didClick);
const ref = useRef(/** @type {HTMLUListElement|null} */ (null));
useOnMiddleClick(ref, didClick);

const documentVisibility = useDocumentVisibility();
const { isReducedMotion } = useEnv();
Expand All @@ -90,7 +89,7 @@ function ActivityBody({ canBurn }) {

return (
<Fragment>
<ul class={styles.activity} ref={auxClickRef} onClick={didClick} data-busy={busy}>
<ul class={styles.activity} ref={ref} onClick={didClick} data-busy={busy}>
{keys.value.available.map((id, index) => {
if (canBurn && !isReducedMotion) return <BurnableItem id={id} key={id} documentVisibility={documentVisibility} />;
return <RemovableItem id={id} key={id} canBurn={canBurn} documentVisibility={documentVisibility} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { TileRow } from './TileRow.js';
import { FavoritesContext } from './FavoritesProvider.js';
import { CustomizerContext, CustomizerThemesContext } from '../../customizer/CustomizerProvider.js';
import { signal, useComputed } from '@preact/signals';
import { eventToTarget, useAuxClick, useDocumentVisibility } from '../../utils.js';
import { eventToTarget, useOnMiddleClick, useDocumentVisibility } from '../../utils.js';

/**
* @typedef {import('../../../types/new-tab.js').Expansion} Expansion
Expand Down Expand Up @@ -153,9 +153,8 @@ function VirtualizedGridRows({ WIDGET_ID, rowHeight, favorites, expansion, openF
? rowHeight
: rows.length * rowHeight;

// handle middle clicks (preact doesn't seem to support `onAuxClick` out of the box)
const clickHandler = getOnClickHandler(openFavorite, platformName);
useAuxClick(safeAreaRef, clickHandler);
useOnMiddleClick(safeAreaRef, clickHandler);

return (
<div
Expand Down
8 changes: 4 additions & 4 deletions special-pages/pages/new-tab/app/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ export function useDocumentVisibility() {
}

/**
* Custom hook to handle auxclick event on an element.
* Custom hook to handle middle click event on an element. This is required because Preact doens't support the auxclick event.
* @param {import('preact').RefObject<HTMLElement>} ref - The ref of the element to attach the event listener to.
* @param {Function} handler - The function to execute on auxclick event.
* @param {Function} handler - The function to execute on the middle click event.
*/
export function useAuxClick(ref, handler) {
export function useOnMiddleClick(ref, handler) {
useEffect(() => {
const element = ref.current;
if (!element) return;

const handleAuxClick = (event) => handler(event);
const handleAuxClick = (event) => event.button === 1 /* middle button */ && handler(event);

element.addEventListener('auxclick', handleAuxClick);

Expand Down

0 comments on commit a46c78a

Please sign in to comment.