diff --git a/src/entities/Breadcrumb/model/services/handleTitleStatusChanged/handleTitleStatusChanged.ts b/src/entities/Breadcrumb/model/services/handleTitleStatusChanged/handleTitleStatusChanged.ts new file mode 100644 index 0000000..7e53688 --- /dev/null +++ b/src/entities/Breadcrumb/model/services/handleTitleStatusChanged/handleTitleStatusChanged.ts @@ -0,0 +1,29 @@ +import type { TitleStatusChangeHandler } from '$shared/ui'; +import type { Snippet } from 'svelte'; +import { scrollBreadcrumbsStore } from '../../store/scrollBreadcrumbsStore.svelte'; + +/** + * Updates the breadcrumb store when the title visibility status changes. + * + * @param index - Index of the section + * @param isPast - Whether the section is past the current scroll position + * @param title - Snippet for a title itself + * @param id - ID of the section + * @returns Cleanup callback + */ +export const handleTitleStatusChanged: TitleStatusChangeHandler = ( + index: number, + isPast: boolean, + title?: Snippet<[{ className?: string }]>, + id?: string, +) => { + if (isPast && title) { + scrollBreadcrumbsStore.add({ index, title, id }); + } else { + scrollBreadcrumbsStore.remove(index); + } + + return () => { + scrollBreadcrumbsStore.remove(index); + }; +};