Skip to content

Commit

Permalink
docs: fix the edit link example jumping page scroll. Fixes #1571 (#1589)
Browse files Browse the repository at this point in the history
autoFocus within a floating wrapper appears to cause a page scroll, add workaround
  • Loading branch information
whawker committed Apr 8, 2022
1 parent f94a28b commit c25830e
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/storybook-react/stories/extension-link/edit-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useEffect, useLayoutEffect, useMemo, useState } from 'react';
import type { ChangeEvent, HTMLProps, KeyboardEvent } from 'react';
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { createMarkPositioner, LinkExtension, ShortcutHandlerProps } from 'remirror/extensions';
import {
ComponentItem,
Expand Down Expand Up @@ -109,6 +110,26 @@ function useFloatingLinkState() {
);
}

const DelayAutoFocusInput = ({ autoFocus, ...rest }: HTMLProps<HTMLInputElement>) => {
const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (!autoFocus) {
return;
}

const frame = window.requestAnimationFrame(() => {
inputRef.current?.focus();
});

return () => {
window.cancelAnimationFrame(frame);
};
}, [autoFocus]);

return <input ref={inputRef} {...rest} />;
};

const FloatingLinkToolbar = () => {
const { isEditing, linkPositioner, clickEdit, onRemove, submitHref, href, setHref, cancelHref } =
useFloatingLinkState();
Expand Down Expand Up @@ -149,13 +170,13 @@ const FloatingLinkToolbar = () => {
enabled={isEditing}
renderOutsideEditor
>
<input
<DelayAutoFocusInput
style={{ zIndex: 20 }}
autoFocus
placeholder='Enter link...'
onChange={(event) => setHref(event.target.value)}
onChange={(event: ChangeEvent<HTMLInputElement>) => setHref(event.target.value)}
value={href}
onKeyPress={(event) => {
onKeyPress={(event: KeyboardEvent<HTMLInputElement>) => {
const { code } = event;

if (code === 'Enter') {
Expand Down

1 comment on commit c25830e

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉 Published on https://remirror.io as production
🚀 Deployed on https://6250a45bdc9353104e4341ba--remirror.netlify.app

Please sign in to comment.