-
Notifications
You must be signed in to change notification settings - Fork 1
[Feature] Add a Carousel component #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
titouanmathis
wants to merge
16
commits into
develop
Choose a base branch
from
feature/carousel
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c02406b
Add a Carousel component
titouanmathis 3251909
Add docs
titouanmathis 22828d5
Add tests
titouanmathis 2953816
Fix implementation
titouanmathis d2b70ed
Update docs
titouanmathis 9e96a51
Cache the CarouselItem state
titouanmathis 6d103f5
Fix starting the ticked service
titouanmathis b9b5251
Fix tests
titouanmathis 62e21f9
Refactor progress to the root component
titouanmathis a5337da
Update tests
titouanmathis d9f1f49
Update docs
titouanmathis dd5c869
Add an Indexable primitive component and a withIndex decorator
antoine4livre 201446a
Use Indexable for the Carousel
antoine4livre d2b5ad9
Fix @titouanmathis' PR feedbacks
antoine4livre d9da3a4
Use strict equality in carousel progress comparison
antoine4livre c68468c
Ignore type error of the return of withIndex decorator
antoine4livre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --- | ||
| title: Carousel examples | ||
| --- | ||
|
|
||
| # Examples | ||
|
|
||
| ## Horizontal | ||
|
|
||
| <PreviewPlayground | ||
| :html="() => import('./stories/horizontal/app.twig')" | ||
| :html-editor="false" | ||
| :script="() => import('./stories/horizontal/app.js?raw')" | ||
| :script-editor="false" | ||
| /> | ||
|
|
||
| ## Vertical | ||
|
|
||
| <PreviewPlayground | ||
| :html="() => import('./stories/vertical/app.twig')" | ||
| :html-editor="false" | ||
| :script="() => import('./stories/vertical/app.js?raw')" | ||
| :script-editor="false" | ||
| /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| --- | ||
| badges: [JS] | ||
| --- | ||
|
|
||
| # Carousel <Badges :texts="$frontmatter.badges" /> | ||
|
|
||
| ## Table of content | ||
|
|
||
| - [Examples](./examples.md) | ||
|
|
||
| ## Usage | ||
|
|
||
| Use the `Carousel` component to display a carousel with native scroll capabilities. | ||
|
|
||
| ::: code-group | ||
|
|
||
| ```js twoslash [app.js] | ||
| import { Base, createApp } from '@studiometa/js-toolkit'; | ||
| import { Carousel } from '@studiometa/ui'; | ||
|
|
||
| class App extends Base { | ||
| static config = { | ||
| name: 'App', | ||
| components: { | ||
| Carousel, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export default createApp(App); | ||
| ``` | ||
|
|
||
| ```twig [carousel.twig] | ||
| <div data-component="Carousel"> | ||
| <div data-component="CarouselWrapper CarouselDrag" class="whitespace-nowrap"> | ||
| {% for item in 1..4 %} | ||
| <div data-component="CarouselItem" class="inline-block"> | ||
| #{{ item }} | ||
| </div> | ||
| {% endfor %} | ||
| </div> | ||
| </div> | ||
| ``` | ||
|
|
||
| ::: |
13 changes: 13 additions & 0 deletions
13
packages/docs/components/Carousel/stories/horizontal/app.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { Base, createApp } from '@studiometa/js-toolkit'; | ||
| import { Carousel } from '@studiometa/ui'; | ||
|
|
||
| class App extends Base { | ||
| static config = { | ||
| name: 'App', | ||
| components: { | ||
| Carousel, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| createApp(App); |
56 changes: 56 additions & 0 deletions
56
packages/docs/components/Carousel/stories/horizontal/app.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| {% set colors = ['red', 'green', 'blue', 'purple'] %} | ||
| {% set count = 5 %} | ||
|
|
||
| <div data-component="Carousel" class="grid gap-10 p-10" data-option-axis="x"> | ||
| <div data-component="CarouselWrapper CarouselDrag" | ||
| class="flex items-center gap-8 w-full overflow-x-auto snap-x snap-mandatory" | ||
| style="scrollbar-width: none;"> | ||
| {% for i in 1..count %} | ||
| {% set color = colors[loop.index0 % (colors|length)] %} | ||
| <div data-component="CarouselItem" | ||
| class=" | ||
| snap-center shrink-0 flex items-center justify-center | ||
| w-1/3 h-48 bg-{{ | ||
| color | ||
| }}-400 ring-inset ring-{{ | ||
| color | ||
| }}-600 | ||
| ring | ||
| text-white font-bold rounded-xl | ||
| " | ||
| style="--tw-ring-opacity: var(--carousel-item-active, 0)"> | ||
| NΒ°{{ i }} | ||
| </div> | ||
| {% endfor %} | ||
| </div> | ||
| <div class="relative w-full h-2 rounded-full bg-gray-200 overflow-hidden"> | ||
| <div style="--tw-translate-x: calc((var(--carousel-progress, 0) - 1) * 100%)" | ||
| class="absolute inset-0 -translate-x-full bg-black rounded-full"></div> | ||
| </div> | ||
| <nav class="flex items-center justify-center gap-2"> | ||
| {% include '@ui/Button/StyledButton.twig' with { | ||
| label: 'β Prev', | ||
| attr: { | ||
| data_component: 'CarouselBtn', | ||
| data_option_action: 'prev' | ||
| } | ||
| } %} | ||
| {% for i in 1..count %} | ||
| {% include '@ui/Button/StyledButton.twig' with { | ||
| label: i, | ||
| theme: 'secondary', | ||
| attr: { | ||
| data_component: 'CarouselBtn', | ||
| data_option_action: loop.index0 | ||
| } | ||
| } %} | ||
| {% endfor %} | ||
| {% include '@ui/Button/StyledButton.twig' with { | ||
| label: 'Next β', | ||
| attr: { | ||
| data_component: 'CarouselBtn', | ||
| data_option_action: 'next' | ||
| } | ||
| } %} | ||
| </nav> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { Base, createApp } from '@studiometa/js-toolkit'; | ||
| import { Carousel } from '@studiometa/ui'; | ||
|
|
||
| class App extends Base { | ||
| static config = { | ||
| name: 'App', | ||
| components: { | ||
| Carousel, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| createApp(App); |
58 changes: 58 additions & 0 deletions
58
packages/docs/components/Carousel/stories/vertical/app.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| {% set colors = ['red', 'green', 'blue', 'purple'] %} | ||
| {% set count = 5 %} | ||
|
|
||
| <div data-component="Carousel" | ||
| class="flex items-center justify-start gap-10 p-10" | ||
| data-option-axis="y"> | ||
| <div data-component="CarouselWrapper CarouselDrag" | ||
| class="grid gap-4 w-[max-content] h-52 overflow-y-auto snap-y snap-mandatory" | ||
| style="scrollbar-width: none;"> | ||
| {% for i in 1..count %} | ||
| {% set color = colors[loop.index0 % (colors|length)] %} | ||
| <div data-component="CarouselItem" | ||
| class=" | ||
| snap-center shrink-0 flex items-center justify-center self-center justify-self-center | ||
| w-24 h-24 bg-{{ | ||
| color | ||
| }}-400 ring-inset ring-{{ | ||
| color | ||
| }}-600 | ||
| ring | ||
| text-white font-bold rounded-xl | ||
| " | ||
| style="--tw-ring-opacity: var(--carousel-item-active, 0)"> | ||
| NΒ°{{ i }} | ||
| </div> | ||
| {% endfor %} | ||
| </div> | ||
| <div class="relative h-52 w-2 rounded-full bg-gray-200 overflow-hidden"> | ||
| <div style="--tw-translate-y: calc((var(--carousel-progress, 0) - 1) * 100%)" | ||
| class="absolute inset-0 -translate-y-full bg-black rounded-full"></div> | ||
| </div> | ||
| <nav class="grid items-center justify-center gap-2"> | ||
| {% include '@ui/Button/StyledButton.twig' with { | ||
| label: 'β', | ||
| attr: { | ||
| data_component: 'CarouselBtn', | ||
| data_option_action: 'prev' | ||
| } | ||
| } %} | ||
| {% for i in 1..count %} | ||
| {% include '@ui/Button/StyledButton.twig' with { | ||
| label: i, | ||
| theme: 'secondary', | ||
| attr: { | ||
| data_component: 'CarouselBtn', | ||
| data_option_action: loop.index0 | ||
| } | ||
| } %} | ||
| {% endfor %} | ||
| {% include '@ui/Button/StyledButton.twig' with { | ||
| label: 'β', | ||
| attr: { | ||
| data_component: 'CarouselBtn', | ||
| data_option_action: 'next' | ||
| } | ||
| } %} | ||
| </nav> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from 'compute-scroll-into-view'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { describe, it, expect, vi } from 'vitest'; | ||
| import { AbstractCarouselChild, Carousel } from '@studiometa/ui'; | ||
| import { h, mount, destroy } from '#test-utils'; | ||
|
|
||
| describe('The AbstractCarouselChild class', () => { | ||
| it('should not mount if it can not find a parent Carousel', async () => { | ||
| const div = h('div'); | ||
| const child = new AbstractCarouselChild(div); | ||
| await mount(child); | ||
| expect(child.$isMounted).toBe(false); | ||
| }); | ||
|
|
||
| it('should listen to its parent events and dispatch them', async () => { | ||
| const childElement = h('div'); | ||
| const carouselElement = h('div', [childElement]); | ||
| const carousel = new Carousel(carouselElement); | ||
| const child = new AbstractCarouselChild(childElement); | ||
| await mount(carousel, child); | ||
| expect(child.carousel).toBe(carousel); | ||
| const spy = vi.spyOn(child, '$emit'); | ||
|
|
||
| for (const eventName of ['index', 'progress']) { | ||
| carousel.$emit(eventName, 0); | ||
| expect(spy).toHaveBeenCalledExactlyOnceWith(`parent-carousel-${eventName}`, 0); | ||
| spy.mockClear(); | ||
| } | ||
|
|
||
| await destroy(child); | ||
| spy.mockClear(); | ||
|
|
||
| for (const eventName of ['index', 'progress']) { | ||
| carousel.$emit(eventName, 0); | ||
| expect(spy).not.toHaveBeenCalled(); | ||
| spy.mockClear(); | ||
| } | ||
| }); | ||
|
|
||
| it('should expose the parent carousel isHorizontal and isVertical getters', async () => { | ||
| const childElement = h('div'); | ||
| const carouselElement = h('div', [childElement]); | ||
| const carousel = new Carousel(carouselElement); | ||
| const child = new AbstractCarouselChild(childElement); | ||
| await mount(carousel, child); | ||
| expect(child.isHorizontal).toBe(carousel.isHorizontal); | ||
| expect(child.isVertical).toBe(carousel.isVertical); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { describe, it, expect, vi } from 'vitest'; | ||
| import { Carousel } from '@studiometa/ui'; | ||
| import { h, mount, wait } from '#test-utils'; | ||
|
|
||
| describe('The Carousel class', () => { | ||
| it('should have an axis option', async () => { | ||
| const div = h('div'); | ||
| const carousel = new Carousel(div); | ||
| await mount(carousel); | ||
| expect(carousel.isHorizontal).toBe(true); | ||
| expect(carousel.isVertical).toBe(false); | ||
| div.setAttribute('data-option-axis', 'y'); | ||
| expect(carousel.isHorizontal).toBe(false); | ||
| expect(carousel.isVertical).toBe(true); | ||
| }); | ||
|
|
||
| it('should emit index and progress events', async () => { | ||
| const items = [ | ||
| h('div', { dataComponent: 'CarouselItem' }), | ||
| h('div', { dataComponent: 'CarouselItem' }), | ||
| ]; | ||
| const wrapper = h('div', { dataComponent: 'CarouselWrapper' }, items); | ||
| const div = h('div', [wrapper]); | ||
| const carousel = new Carousel(div); | ||
| await mount(carousel); | ||
| const indexFn = vi.fn(); | ||
| const progressFn = vi.fn(); | ||
| carousel.$on('index', indexFn); | ||
| carousel.$on('progress', progressFn); | ||
| carousel.goTo(0); | ||
| expect(indexFn).toHaveBeenCalledOnce(); | ||
| expect(indexFn.mock.lastCall[0].detail).toEqual([0]); | ||
| await wait(); | ||
| expect(progressFn).toHaveBeenCalledOnce(); | ||
| progressFn.mockClear(); | ||
| carousel.goTo(1); | ||
| expect(indexFn.mock.lastCall[0].detail).toEqual([1]); | ||
| }); | ||
|
|
||
| it('should implement an indexable API', async () => { | ||
| const items = [ | ||
| h('div', { dataComponent: 'CarouselItem' }), | ||
| h('div', { dataComponent: 'CarouselItem' }), | ||
| h('div', { dataComponent: 'CarouselItem' }), | ||
| h('div', { dataComponent: 'CarouselItem' }), | ||
| ]; | ||
| const wrapper = h('div', { dataComponent: 'CarouselWrapper' }, items); | ||
| const div = h('div', [wrapper]); | ||
| const carousel = new Carousel(div); | ||
| await mount(carousel); | ||
|
|
||
| expect(carousel.currentIndex).toBe(0); | ||
| expect(carousel.prevIndex).toBe(0); | ||
| expect(carousel.nextIndex).toBe(1); | ||
| expect(carousel.lastIndex).toBe(3); | ||
|
|
||
| carousel.goTo(1); | ||
|
|
||
| expect(carousel.currentIndex).toBe(1); | ||
| expect(carousel.prevIndex).toBe(0); | ||
| expect(carousel.nextIndex).toBe(2); | ||
| expect(carousel.lastIndex).toBe(3); | ||
|
|
||
| carousel.goNext(); | ||
|
|
||
| expect(carousel.currentIndex).toBe(2); | ||
| expect(carousel.prevIndex).toBe(1); | ||
| expect(carousel.nextIndex).toBe(3); | ||
| expect(carousel.lastIndex).toBe(3); | ||
|
|
||
| carousel.goPrev(); | ||
|
|
||
| expect(carousel.currentIndex).toBe(1); | ||
| expect(carousel.prevIndex).toBe(0); | ||
| expect(carousel.nextIndex).toBe(2); | ||
| expect(carousel.lastIndex).toBe(3); | ||
| }); | ||
|
|
||
| it('should go to the current index on mount', async () => { | ||
| const div = h('div'); | ||
| const carousel = new Carousel(div); | ||
| const spy = vi.spyOn(carousel, 'goTo'); | ||
| await mount(carousel); | ||
| expect(spy).toHaveBeenCalledExactlyOnceWith(carousel.currentIndex); | ||
| }); | ||
|
|
||
| it('should go to the current index on resize', async () => { | ||
| const div = h('div'); | ||
| const carousel = new Carousel(div); | ||
| const spy = vi.spyOn(carousel, 'goTo'); | ||
| carousel.resized(); | ||
| expect(spy).toHaveBeenCalledExactlyOnceWith(carousel.currentIndex); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.