Skip to content

Remove passive event "polyfill" and unused randomElement() utility #1653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useMemo, useRef, useEffect, useState } from 'react';
import { useLocation } from '@reach/router';
import { passiveEventArg } from '../utils';

/**
* Takes an array of images nodes and makes a hashed object based on their names
Expand Down Expand Up @@ -94,10 +93,11 @@ export const usePersistScrollPosition = (key, _namespace) => {
}

lastKey = key;
current.addEventListener('scroll', onScroll, passiveEventArg);

current.addEventListener('scroll', onScroll, { passive: true });

return () => {
current.removeEventListener('scroll', onScroll, passiveEventArg);
current.removeEventListener('scroll', onScroll);
};
}, [key, namespace]);

Expand Down Expand Up @@ -125,7 +125,7 @@ export const getReadableDate = (dateString) => {

/**
* This hook will return true for the first render on the server and for the
* first render on the client. Otherwhise, it will return false.
* first render on the client. Otherwise, it will return false.
*
* This function encapsulates a hook so "rules of hooks" apply to it.
* @see {@link https://reactjs.org/docs/hooks-rules.html}
Expand Down
37 changes: 1 addition & 36 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
import slugify from 'slugify';

// so it doesn't throw if no window
const win =
typeof window !== 'undefined' ? window : { screen: {}, navigator: {} };

// passive events test
// adapted from https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
let passiveOptionAccessed = false;
const options = {
get passive() {
return (passiveOptionAccessed = true);
}
};

// have to set and remove a no-op listener instead of null
// (which was used previously), because Edge v15 throws an error
// when providing a null callback.
// https://github.com/rafgraph/detect-passive-events/pull/3
const noop = () => {};
win.addEventListener && win.addEventListener('p', noop, options);
win.removeEventListener && win.removeEventListener('p', noop, false);

export const supportsPassiveEvents = passiveOptionAccessed;

export const passiveEventArg = supportsPassiveEvents
? { capture: false, passive: true }
: false;

export const stringValueOrAll = (str) => {
return typeof str === 'string' && str !== '' ? str : 'all';
};
Expand Down Expand Up @@ -70,16 +43,8 @@ export const shuffleCopy = (array) => {
return copy;
};

export const randomElement = (array) => {
if (array.length === 0) {
return null;
}
const index = Math.floor(Math.random() * array.length);
return array[index];
};

/**
* Creates a URL hash value (fragment) refering to a specific part of a
* Creates a URL hash value (fragment) referring to a specific part of a
* multi-part coding challenge. Part 1 of a challenge has no hash value.
*
* @param partIndex {number} Zero-based part index
Expand Down
Loading