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

fix(elements): avoid exception when window is undefined #23324

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/elements/src/utils.ts
Expand Up @@ -21,7 +21,7 @@ export const scheduler = {
* Returns a function that when executed will cancel the scheduled function.
*/
schedule(taskFn: () => void, delay: number): () =>
void{const id = window.setTimeout(taskFn, delay); return () => window.clearTimeout(id);},
void{const id = setTimeout(taskFn, delay); return () => clearTimeout(id);},

/**
* Schedule a callback to be called before the next render.
Expand All @@ -32,6 +32,11 @@ export const scheduler = {
scheduleBeforeRender(taskFn: () => void): () => void{
// TODO(gkalpak): Implement a better way of accessing `requestAnimationFrame()`
// (e.g. accounting for vendor prefix, SSR-compatibility, etc).
if (typeof window === 'undefined') {
// For SSR just schedule immediately.
return scheduler.schedule(taskFn, 0);
}

if (typeof window.requestAnimationFrame === 'undefined') {
const frameMs = 16;
return scheduler.schedule(taskFn, frameMs);
Expand Down