Skip to content
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

fixing issue with announcer and hot module reloading #1585

Merged
merged 5 commits into from
Nov 8, 2019
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
1 change: 1 addition & 0 deletions docs/support/media.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This page contains a list of articles, blogs and newsletters that `react-beautif

## Newsletters

- Tiny letter [November 04, 2019](https://tinyletter.com/cassidoo/letters/the-world-is-changed-by-your-example-not-by-your-opinion-paulo-coelho)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated :D

- Ponyfoo [issue 78](https://ponyfoo.com/weekly/78/javascript-and-css-engines-pwa-drag-and-drop-web-components-and-http-2)
- PonyFoo [issue 99](https://ponyfoo.com/weekly/99/react-across-the-universe-typography-load-balancing-and-javascript-frameworks)
- PonyFoo [issue 141](https://ponyfoo.com/weekly/141/http-3-bgp-leaks-react-as-native-dom-typescript-tensorflow-and-all-things-performance)
Expand Down
79 changes: 42 additions & 37 deletions src/view/use-announcer/use-announcer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
import { useRef, useEffect } from 'react';
import { useMemo, useCallback } from 'use-memo-one';
import { invariant } from '../../invariant';
import type { Announce, ContextId } from '../../types';
import { warning } from '../../dev-warning';
import getBodyElement from '../get-body-element';
Expand All @@ -14,42 +13,48 @@ export default function useAnnouncer(contextId: ContextId): Announce {
const id: string = useMemo(() => getId(contextId), [contextId]);
const ref = useRef<?HTMLElement>(null);

useEffect(() => {
invariant(!ref.current, 'Announcement node already mounted');

const el: HTMLElement = document.createElement('div');
ref.current = el;

// identifier
el.id = id;

// Aria live region

// will force itself to be read
el.setAttribute('aria-live', 'assertive');
el.setAttribute('role', 'log');
// must read the whole thing every time
el.setAttribute('aria-atomic', 'true');

// hide the element visually
Object.assign(el.style, visuallyHidden);

// Add to body
getBodyElement().appendChild(el);

return () => {
// unmounting after a timeout to let any annoucements
// during a mount be published
setTimeout(function remove() {
const toBeRemoved: ?HTMLElement = ref.current;
invariant(toBeRemoved, 'Cannot unmount announcement node');

// Remove from body
getBodyElement().removeChild(toBeRemoved);
ref.current = null;
});
};
}, [id]);
useEffect(
function setup() {
const el: HTMLElement = document.createElement('div');
// storing reference for usage in announce
ref.current = el;

// identifier
el.id = id;

// Aria live region

// will force itself to be read
el.setAttribute('aria-live', 'assertive');
el.setAttribute('role', 'log');
// must read the whole thing every time
el.setAttribute('aria-atomic', 'true');

// hide the element visually
Object.assign(el.style, visuallyHidden);

// Add to body
getBodyElement().appendChild(el);

return function cleanup() {
// Not clearing the ref as it might be used by announce before the timeout expires

// unmounting after a timeout to let any announcements
// during a mount be published
setTimeout(function remove() {
// not clearing the ref as it might have been set by a new effect
getBodyElement().removeChild(el);

// if el was the current ref - clear it so that
// we can get a warning if announce is called
if (el === ref.current) {
ref.current = null;
}
});
};
},
[id],
);

const announce: Announce = useCallback((message: string): void => {
const el: ?HTMLElement = ref.current;
Expand Down