Skip to content

Commit ec687bd

Browse files
authored
Merge pull request #1653 from fturmel/PR/passive-events-cleanup
Remove passive event "polyfill" and unused `randomElement()` utility
2 parents 39ffb43 + 0cc3f60 commit ec687bd

File tree

2 files changed

+5
-40
lines changed

2 files changed

+5
-40
lines changed

src/hooks/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useMemo, useRef, useEffect, useState } from 'react';
22
import { useLocation } from '@reach/router';
3-
import { passiveEventArg } from '../utils';
43

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

9695
lastKey = key;
97-
current.addEventListener('scroll', onScroll, passiveEventArg);
96+
97+
current.addEventListener('scroll', onScroll, { passive: true });
9898

9999
return () => {
100-
current.removeEventListener('scroll', onScroll, passiveEventArg);
100+
current.removeEventListener('scroll', onScroll);
101101
};
102102
}, [key, namespace]);
103103

@@ -125,7 +125,7 @@ export const getReadableDate = (dateString) => {
125125

126126
/**
127127
* This hook will return true for the first render on the server and for the
128-
* first render on the client. Otherwhise, it will return false.
128+
* first render on the client. Otherwise, it will return false.
129129
*
130130
* This function encapsulates a hook so "rules of hooks" apply to it.
131131
* @see {@link https://reactjs.org/docs/hooks-rules.html}

src/utils/index.js

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,5 @@
11
import slugify from 'slugify';
22

3-
// so it doesn't throw if no window
4-
const win =
5-
typeof window !== 'undefined' ? window : { screen: {}, navigator: {} };
6-
7-
// passive events test
8-
// adapted from https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
9-
let passiveOptionAccessed = false;
10-
const options = {
11-
get passive() {
12-
return (passiveOptionAccessed = true);
13-
}
14-
};
15-
16-
// have to set and remove a no-op listener instead of null
17-
// (which was used previously), because Edge v15 throws an error
18-
// when providing a null callback.
19-
// https://github.com/rafgraph/detect-passive-events/pull/3
20-
const noop = () => {};
21-
win.addEventListener && win.addEventListener('p', noop, options);
22-
win.removeEventListener && win.removeEventListener('p', noop, false);
23-
24-
export const supportsPassiveEvents = passiveOptionAccessed;
25-
26-
export const passiveEventArg = supportsPassiveEvents
27-
? { capture: false, passive: true }
28-
: false;
29-
303
export const stringValueOrAll = (str) => {
314
return typeof str === 'string' && str !== '' ? str : 'all';
325
};
@@ -70,16 +43,8 @@ export const shuffleCopy = (array) => {
7043
return copy;
7144
};
7245

73-
export const randomElement = (array) => {
74-
if (array.length === 0) {
75-
return null;
76-
}
77-
const index = Math.floor(Math.random() * array.length);
78-
return array[index];
79-
};
80-
8146
/**
82-
* Creates a URL hash value (fragment) refering to a specific part of a
47+
* Creates a URL hash value (fragment) referring to a specific part of a
8348
* multi-part coding challenge. Part 1 of a challenge has no hash value.
8449
*
8550
* @param partIndex {number} Zero-based part index

0 commit comments

Comments
 (0)