Skip to content

Commit

Permalink
chore: replace with useRuntimeVisualRefresh from component toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
YueyingLu committed Sep 19, 2023
1 parent 6d19d70 commit 619925b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 123 deletions.

This file was deleted.

44 changes: 2 additions & 42 deletions src/internal/hooks/use-visual-mode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

import { ALWAYS_VISUAL_REFRESH } from '../../environment';
import { isDevelopment } from '../../is-development';
import { warnOnce } from '@cloudscape-design/component-toolkit/internal';
import { useRuntimeVisualRefresh } from '@cloudscape-design/component-toolkit/internal';

const awsuiVisualRefreshFlag = Symbol.for('awsui-visual-refresh-flag');
interface ExtendedWindow extends Window {
[awsuiVisualRefreshFlag]?: () => boolean;
}
declare const window: ExtendedWindow;

export const useVisualRefresh = ALWAYS_VISUAL_REFRESH ? () => true : useVisualRefreshDynamic;

// We expect VR is to be set only once and before the application is rendered.
let visualRefreshState: undefined | boolean = undefined;

// for testing
export function clearVisualRefreshState() {
visualRefreshState = undefined;
}

function detectVisualRefresh() {
return typeof document !== 'undefined' && !!document.querySelector('.awsui-visual-refresh');
}

export function useVisualRefreshDynamic() {
if (visualRefreshState === undefined) {
visualRefreshState = detectVisualRefresh();
if (!visualRefreshState && typeof window !== 'undefined' && window[awsuiVisualRefreshFlag]?.()) {
document.body.classList.add('awsui-visual-refresh');
visualRefreshState = true;
}
}
if (isDevelopment) {
const newVisualRefreshState = detectVisualRefresh();
if (newVisualRefreshState !== visualRefreshState) {
warnOnce(
'Visual Refresh',
'Dynamic visual refresh change detected. This is not supported. ' +
'Make sure `awsui-visual-refresh` is attached to the `<body>` element before initial React render'
);
}
}
return visualRefreshState;
}
export const useVisualRefresh = ALWAYS_VISUAL_REFRESH ? () => true : useRuntimeVisualRefresh;

0 comments on commit 619925b

Please sign in to comment.