Skip to content

Commit a4859ce

Browse files
Oskii2311Oskar Janczak
and
Oskar Janczak
authored
Preserve query on toggling full page preview, add better types (#581)
Co-authored-by: Oskar Janczak <[email protected]>
1 parent 7afde81 commit a4859ce

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

.changeset/witty-terms-end.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ladle/react": major
3+
---
4+
5+
Toggling full-page preview preserves the search query.

packages/ladle/lib/app/src/app.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ const App = () => {
5656
const initialGlobalState = getUrlState(location.search);
5757
const [globalState, dispatch] = React.useReducer(reducer, initialGlobalState);
5858
const prevGlobalStateRef = React.useRef<Partial<GlobalState>>({});
59+
const [search, setSearch] = React.useState("");
60+
5961
let customBackground = "";
6062
if (globalState.control) {
6163
Object.keys(globalState.control).forEach((key) => {
@@ -177,6 +179,8 @@ const App = () => {
177179
)}
178180
</main>
179181
<Navigation
182+
search={search}
183+
setSearch={setSearch}
180184
stories={stories}
181185
hotkeys={globalState.hotkeys}
182186
story={globalState.story}

packages/ladle/lib/app/src/sidebar/main.tsx

+18-12
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ const Main = ({
1818
story,
1919
updateStory,
2020
hotkeys,
21+
search,
22+
setSearch,
2123
}: {
2224
stories: string[];
2325
story: string;
2426
updateStory: UpdateStory;
2527
hotkeys: boolean;
28+
search: string;
29+
setSearch: React.Dispatch<React.SetStateAction<string>>;
2630
}) => {
27-
const [search, setSearch] = React.useState("");
2831
const [width, setWidth] = React.useState(
2932
getSettings().sidebarWidth || DEFAULT_WIDTH,
3033
);
3134
const [resizeActive, setResizeActive] = React.useState(false);
3235
const handleRef = React.useRef<HTMLDivElement>(null);
33-
const searchEl = React.useRef(null);
36+
const searchEl = React.useRef<HTMLInputElement>(null);
3437
const treeRoot = React.useRef<HTMLUListElement | null>(null);
3538

3639
React.useEffect(() => {
@@ -79,11 +82,10 @@ const Main = ({
7982
};
8083
}, [resizeActive, setResizeActive, setWidth, handleRef.current]);
8184

82-
useHotkeys(
83-
config.hotkeys.search,
84-
() => (searchEl.current as any as HTMLInputElement).focus(),
85-
{ preventDefault: true, enabled: hotkeys },
86-
);
85+
useHotkeys(config.hotkeys.search, () => searchEl.current?.focus(), {
86+
preventDefault: true,
87+
enabled: hotkeys,
88+
});
8789
const canonicalSearch = search
8890
.toLocaleLowerCase()
8991
.replace(new RegExp("\\s+", "g"), "-");
@@ -92,6 +94,14 @@ const Main = ({
9294
story.includes(canonicalSearch),
9395
);
9496

97+
const onKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (e) => {
98+
if (e.key === "ArrowDown" && treeRoot.current?.firstChild) {
99+
const firstLiElement = treeRoot.current.firstChild as HTMLLIElement;
100+
101+
firstLiElement.focus();
102+
}
103+
};
104+
95105
return (
96106
<>
97107
<div
@@ -125,11 +135,7 @@ const Main = ({
125135
aria-label="Search stories"
126136
value={search}
127137
ref={searchEl}
128-
onKeyDown={(e) => {
129-
if (e.key === "ArrowDown") {
130-
(treeRoot as any).current.firstChild.focus();
131-
}
132-
}}
138+
onKeyDown={onKeyDown}
133139
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
134140
setSearch(e.target.value)
135141
}

packages/ladle/lib/app/src/sidebar/tree-view.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const TreeView = ({
3636
}: {
3737
stories: string[];
3838
story: string;
39-
searchRef: React.Ref<HTMLLinkElement>;
39+
searchRef: React.Ref<HTMLInputElement>;
4040
updateStory: UpdateStory;
4141
setTreeRootRef: (root: HTMLUListElement | null) => void;
4242
searchActive?: boolean;

0 commit comments

Comments
 (0)