Files
Ivan Starkov 513d78681b fix: After modifying style, it takes two clicks to select the next instance (#4314)
## Description
Style Source looses focus after adding Token


closes #4309
closes #4306

## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-10-19 15:14:35 +03:00

53 lines
2.2 KiB
TypeScript

let resetTimeoutHandle: number | undefined = undefined;
const resetAutoDisposeInert = () => {
if (resetTimeoutHandle === undefined) {
return;
}
document.body.removeAttribute("inert");
clearTimeout(resetTimeoutHandle);
resetTimeoutHandle = undefined;
};
let lastPointerEventTime = Date.now();
// 1000 ms is a reasonable time for the preview to reset.
// Anyway should never happen after user has finished preview changes (can happen during preview changes)
const AUTO_DISPOSE_INERT_TIMEOUT = 1000;
// A brief delay to ensure mutation observers within the focus scope are activated by the preview changes.
const DISPOSE_INERT_TIMEOUT = 300;
const PREVENT_INERT_TIMEOUT = 100;
const setAutoDisposeInert = (timeout: number) => {
// Some events in the builder can occur after clicking on the canvas (e.g., blur on an input field).
// In such cases, we should prevent 'inert' from being set and allow the selection to complete.
if (Date.now() - lastPointerEventTime < PREVENT_INERT_TIMEOUT) {
return;
}
document.body.setAttribute("inert", "true");
// To prevent a completely non-interactive canvas due to edge cases,
// make sure to clean up preview changes if preview styles fail to reset correctly.
clearTimeout(resetTimeoutHandle);
resetTimeoutHandle = window.setTimeout(resetAutoDisposeInert, timeout);
};
/**
* Controls (e.g., radix focus scope) may inadvertently shift focus from inputs.
* Example: When the user modifies styles or content in the settings panel, the use of a mutation observer with Radix causes the focus to shift to the Radix dialog.
* Currently, there's no way to block focus shifts inside iframes (see https:*github.com/w3c/webappsec-permissions-policy/issues/273 for future updates).
* Workaround: use the `inert` attribute on iframe body to prevent focus changes.
*/
export const setInert = () => setAutoDisposeInert(AUTO_DISPOSE_INERT_TIMEOUT);
export const resetInert = () => setAutoDisposeInert(DISPOSE_INERT_TIMEOUT);
// window.self !== window.top means we are on canvas
if (typeof window !== "undefined" && window.self !== window.top) {
window.addEventListener("pointerdown", () => {
lastPointerEventTime = Date.now();
});
}