Skip to content

Commit b10390e

Browse files
committed
Extract forceDetectWrappedElements() to support IE
1 parent 02d26a1 commit b10390e

6 files changed

Lines changed: 31 additions & 7 deletions

File tree

examples/.browserslistrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
>= 1%
2+
IE >= 11

examples/Bootstrap.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import 'core-js';
22

33
import React, { useEffect, useRef, useState } from 'react';
4-
import { DevTools, useDevTools, wrapChildrenClassName } from 'flex-wrap-layout';
4+
import {
5+
DevTools,
6+
forceDetectWrappedElements,
7+
useDevTools,
8+
wrapChildrenClassName
9+
} from 'flex-wrap-layout';
510
import ReactDOM from 'react-dom';
611

712
import './Bootstrap.html';
@@ -59,11 +64,6 @@ interface Person {
5964
nationality: SomeCountries;
6065
}
6166

62-
function forceDetectWrappedElements() {
63-
// https://stackoverflow.com/q/1818474
64-
window.dispatchEvent(new Event('resize'));
65-
}
66-
6767
function People({ people }: { people: Person[] }) {
6868
const ref = useRef(null);
6969

examples/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"clean": "rm -rf build",
88
"clean:all": "npm run clean && rm -rf node_modules package-lock.json",
99
"build": "npm run clean && webpack --mode development",
10+
"build:prod": "npm run clean && webpack --mode production",
1011
"build:watch": "npm run clean && webpack --mode development --watch",
1112
"test:e2e": "NODE_ENV=test jest --config jest-e2e.config.js --verbose",
1213
"chromium:linux": "~/.cache/ms-playwright/chromium-857950/chrome-linux/chrome",

examples/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ module.exports = (_webpackEnv, _argv) => {
3737
module: {
3838
rules: [
3939
{
40-
test: /\.tsx?$/,
40+
test: /\.(js|tsx?)$/,
41+
// [Babel should not transpile core-js](https://github.com/zloirock/core-js/issues/514#issuecomment-476533317)
42+
exclude: /\/core-js/,
4143
loader: 'babel-loader'
4244
},
4345
{

src/forceDetectWrappedElements.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function forceDetectWrappedElements() {
2+
try {
3+
// https://stackoverflow.com/q/1818474
4+
window.dispatchEvent(new Event('resize'));
5+
} catch {
6+
// FIXME Event constructor is not supported by IE
7+
// https://github.com/zloirock/core-js/issues/354
8+
// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent#polyfill
9+
// https://github.com/lifaon74/events-polyfill/blob/bc989dc00fb90a42ae9c012c309d48710dec2589/event-constructor-polyfill.js#L11-L28
10+
// https://github.com/kumarharsh/custom-event-polyfill/blob/v1.0.7/polyfill.js
11+
// http://youmightnotneedjquery.com/#trigger_native
12+
const event = document.createEvent('Event');
13+
const bubbles = false; // Default is false for new Event()
14+
const cancelable = false; // Default is false for new Event()
15+
event.initEvent('resize', bubbles, cancelable);
16+
window.dispatchEvent(event);
17+
}
18+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './detectWrappedElements';
22
export * from './DevTools';
3+
export * from './forceDetectWrappedElements';
34
export * from './useDetectWrappedElements';

0 commit comments

Comments
 (0)