green on focus, scroll fix

This commit is contained in:
Sebastian 2022-04-13 13:46:51 -03:00
parent 38e9e8fea2
commit 5044db6595
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1

View File

@ -86,6 +86,12 @@ const SideBar = styled.div`
dd:nth-child(odd) { dd:nth-child(odd) {
background-color: lightblue; background-color: lightblue;
} }
a {
color: black;
}
dd[data-selected] {
background-color: green;
}
} }
} }
`; `;
@ -175,12 +181,16 @@ function getContentForExample(item: ExampleItem | undefined): () => VNode {
function ExampleList({ function ExampleList({
name, name,
list, list,
selected,
onSelectStory,
}: { }: {
name: string; name: string;
list: { list: {
name: string; name: string;
examples: ExampleItem[]; examples: ExampleItem[];
}[]; }[];
selected: ExampleItem | undefined;
onSelectStory: (i: ExampleItem, id: string) => void;
}): VNode { }): VNode {
const [open, setOpen] = useState(true); const [open, setOpen] = useState(true);
return ( return (
@ -194,9 +204,22 @@ function ExampleList({
{k.examples.map((r) => { {k.examples.map((r) => {
const e = encodeURIComponent; const e = encodeURIComponent;
const eId = `${e(r.group)}-${e(r.component)}-${e(r.name)}`; const eId = `${e(r.group)}-${e(r.component)}-${e(r.name)}`;
const isSelected =
selected &&
selected.component === r.component &&
selected.group === r.group &&
selected.name === r.name;
return ( return (
<dd id={eId} key={r.name}> <dd id={eId} key={r.name} data-selected={isSelected}>
<a href={`#${eId}`}>{r.name}</a> <a
href={`#${eId}`}
onClick={(e) => {
e.preventDefault();
onSelectStory(r, eId);
}}
>
{r.name}
</a>
</dd> </dd>
); );
})} })}
@ -278,9 +301,9 @@ function ErrorReport({
return <Fragment>{children}</Fragment>; return <Fragment>{children}</Fragment>;
} }
function getSelectionFromLocationHash(): ExampleItem | undefined { function getSelectionFromLocationHash(hash: string): ExampleItem | undefined {
if (!location.hash) return undefined; if (!hash) return undefined;
const parts = location.hash.substring(1).split("-"); const parts = hash.substring(1).split("-");
if (parts.length < 3) return undefined; if (parts.length < 3) return undefined;
return findByGroupComponentName( return findByGroupComponentName(
decodeURIComponent(parts[0]), decodeURIComponent(parts[0]),
@ -290,27 +313,20 @@ function getSelectionFromLocationHash(): ExampleItem | undefined {
} }
function Application(): VNode { function Application(): VNode {
const initialSelection = getSelectionFromLocationHash(); const initialSelection = getSelectionFromLocationHash(location.hash);
const [selected, updateSelected] = useState<ExampleItem | undefined>( const [selected, updateSelected] = useState<ExampleItem | undefined>(
initialSelection, initialSelection,
); );
function updateSelectedFromHashChange(): void {
const selected = getSelectionFromLocationHash();
updateSelected(selected);
}
useEffect(() => { useEffect(() => {
window.addEventListener("hashchange", updateSelectedFromHashChange);
if (location.hash) { if (location.hash) {
const hash = location.hash.substring(1); const hash = location.hash.substring(1);
const found = document.getElementById(hash); const found = document.getElementById(hash);
if (found) { if (found) {
found.scrollIntoView(); found.scrollIntoView({
block: "center",
});
} }
} }
return () => {
window.removeEventListener("hashchange", updateSelectedFromHashChange);
};
}, []); }, []);
const ExampleContent = getContentForExample(selected); const ExampleContent = getContentForExample(selected);
@ -321,7 +337,18 @@ function Application(): VNode {
<Page> <Page>
<SideBar> <SideBar>
{allExamples.map((e) => ( {allExamples.map((e) => (
<ExampleList key={e.title} name={e.title} list={e.list} /> <ExampleList
key={e.title}
name={e.title}
list={e.list}
selected={selected}
onSelectStory={(item, htmlId) => {
document.getElementById(htmlId)?.scrollIntoView({
block: "center",
});
updateSelected(item);
}}
/>
))} ))}
<hr /> <hr />
</SideBar> </SideBar>