diff --git a/components/src/Scroller/Scroller.mdx b/components/src/Scroller/Scroller.mdx new file mode 100644 index 00000000..5d764a22 --- /dev/null +++ b/components/src/Scroller/Scroller.mdx @@ -0,0 +1,25 @@ +import { Controls, Meta } from '@storybook/addon-docs/blocks'; + +import * as ScrollerStories from './Scroller.stories.svelte'; + + + +# Scroller + +A scroller component for Svelte apps, based on svelte-scroller by Rich Harris ([license](#original-license)) but using a different approach to positioning (`position: sticky`). + +## Properties + + + +## Original license + +Copyright (c) 2018 Rich Harris + +Permission is hereby granted by the authors of this software, to any person, to use the software for any purpose, free of charge, including the rights to run, read, copy, change, distribute and sell it, and including usage rights to any patents the authors may hold on it, subject to the following conditions: + +This license, or a link to its text, must be included with all copies of the software and any derivative works. + +Any modification to the software submitted to the authors may be incorporated into the software under the terms of this license. + +The software is provided "as is", without warranty of any kind, including but not limited to the warranties of title, fitness, merchantability and non-infringement. The authors have no obligation to provide support or updates for the software, and may not be held liable for any damages, claims or other liability arising from its use. diff --git a/components/src/Scroller/Scroller.stories.svelte b/components/src/Scroller/Scroller.stories.svelte new file mode 100644 index 00000000..7015ad72 --- /dev/null +++ b/components/src/Scroller/Scroller.stories.svelte @@ -0,0 +1,101 @@ + + + { + await step('Verify state values are set', async () => { + expect(index).toBeGreaterThanOrEqual(0); + expect(offset).toBeGreaterThanOrEqual(0); + expect(progress).toBeGreaterThanOrEqual(0); + }); + + await step('Foreground content is rendered', async () => { + const sections = canvasElement.querySelectorAll('svelte-scroller-foreground section'); + expect(sections).toHaveLength(5); + expect(sections[0]).toHaveTextContent('This is the first section.'); + }); + + await step('Background content is rendered', async () => { + const background = canvasElement.querySelector('svelte-scroller-background'); + expect(background).toBeDefined(); + expect(background).toHaveTextContent('This is the background content'); + }); + + await step('Index is updated when scrolling', async () => { + // Store the event handler function separately + const scrollEndHandler = () => { + expect(index).toBe(3); + // Clean up the event listener after test has run + window.removeEventListener('scrollend', scrollEndHandler); + // Reset scroll position to top after test + window.scrollTo(0, 0); + }; + + // Add the event listener with the stored function reference + window.addEventListener('scrollend', scrollEndHandler); + + const sections = canvasElement.querySelectorAll('svelte-scroller-foreground section'); + sections[3].scrollIntoView(); + }); + }} +> +
+ + {#snippet background()} +

+ This is the background content. It will stay fixed in place while the foreground scrolls + over the top. +

+

+ Section {index + 1} is currently active. +

+ +

index / {index}

+

offset / {offset}

+

progress / {progress}

+ {/snippet} + + {#snippet foreground()} +
This is the first section.
+
This is the second section.
+
This is the third section.
+
This is the fourth section.
+
This is the fifth section.
+ {/snippet} +
+
+
+ + diff --git a/components/src/Scroller/Scroller.svelte b/components/src/Scroller/Scroller.svelte new file mode 100644 index 00000000..f6ed8daa --- /dev/null +++ b/components/src/Scroller/Scroller.svelte @@ -0,0 +1,339 @@ + + + + + + + + + + {@render background()} + + + + + {@render foreground()} + + + + diff --git a/components/src/Scroller/index.ts b/components/src/Scroller/index.ts new file mode 100644 index 00000000..7603e1f7 --- /dev/null +++ b/components/src/Scroller/index.ts @@ -0,0 +1,2 @@ +import Scroller from './Scroller.svelte'; +export default Scroller; diff --git a/components/src/index.js b/components/src/index.js index 347844e9..b93bed42 100644 --- a/components/src/index.js +++ b/components/src/index.js @@ -11,6 +11,7 @@ export { default as Note } from './Note/Note.svelte'; // Display export { default as Card } from './Card/Card.svelte'; +export { default as Scroller } from './Scroller/Scroller.svelte'; // Chart export { default as ChartHeader } from './ChartHeader/ChartHeader.svelte';