Replies: 2 comments 2 replies
-
I wound up with something like this: import {
Accessor,
createContext,
createSignal,
For,
onCleanup,
onMount,
ParentProps,
Show,
useContext,
} from 'solid-js';
import { A } from 'solid-start';
type Crumb = {
title: string;
href: string;
};
const BreadcrumbContext =
createContext<
readonly [
Accessor<Array<Crumb>>,
{ addCrumb: (crumb: Crumb) => void; removeCrumb: (crumb: Crumb) => void },
]
>();
export function BreadcrumbProvider(props: ParentProps) {
const [crumbs, setCrumbs] = createSignal<Array<Crumb>>([]);
const value = [
crumbs,
{
addCrumb(crumb: Crumb) {
setCrumbs((a) => [...a, crumb]);
},
removeCrumb({ title, href }: Crumb) {
setCrumbs((a) => a.filter((c) => c.title !== title && c.href !== href));
},
},
] as const;
return (
<BreadcrumbContext.Provider value={value}>
{props.children}
</BreadcrumbContext.Provider>
);
}
export function useCrumb(crumb: Crumb) {
const [, { addCrumb, removeCrumb }] = useContext(BreadcrumbContext)!;
onMount(() => addCrumb(crumb));
onCleanup(() => removeCrumb(crumb));
}
export default function PageBreadcrumbs() {
const [crumbs] = useContext(BreadcrumbContext)!;
const lastCrumb = () => {
const list = crumbs();
return list[Math.max(list.length - 1, 0)];
};
const initialCrumbs = () => {
const list = crumbs();
return list.slice(0, -1);
};
return (
<Show when={crumbs().length > 1}>
{/* render breadcrumbs */}
</Show>
);
} In my route components I just call
Does anyone have any ideas on how to update this to be SSR-friendly? |
Beta Was this translation helpful? Give feedback.
0 replies
-
If you are on the server, instead of using lifecycle events, store the current crumbs in the request event immediately, and forgo the cleanup. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Does anyone have an example of how one might produce automatically-generated breadcrumbs in Solid Start? In Remix I would use
export const meta
to export some metainformation (like a breadcrumb title) and then in a top-level layout I could take all of that along with the associated href values and assemble breadcrumbs.So far I'm thinking I would have to store some sort of map for this meta information in a layout breadcrumbs component and have it use
useLocation()
to determine what to render.Beta Was this translation helpful? Give feedback.
All reactions