-
Notifications
You must be signed in to change notification settings - Fork 13.4k
feat(segment): add segment-view and segment-content components #29882
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
Closed
Closed
Changes from 11 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
fa74077
fix(segment): animate the highlight with value changes
brandyscarney 0324a78
feat(segment): add segment view and content components
brandyscarney 8af4d74
style: lint
brandyscarney 00c378f
refactor(segment): link the button and content with content-id and id
brandyscarney 44a0855
refactor(segment): remove uneccessary function
brandyscarney 678990d
feat(segment-view): support disabled
brandyscarney 44e8374
fix(segment): only call updateSegmentView when gesture ends or button…
brandyscarney ba28530
fix(segment): set the view to the initial value without scrolling
brandyscarney 686d943
test(segment-view): add a test for the proper content being displayed
brandyscarney e6f76d5
style: lint
brandyscarney 9103c40
docs(segment-view): document setContent method and add example
brandyscarney 4c0407e
test(segment-view): fix test
brandyscarney d8f27d8
test(segment-view): update function for clearing segment value
brandyscarney faa7065
feat(segment-content): add disabled prop and hide the content
brandyscarney 5401e8d
test(segment-view): split out disabled segment view / content test
brandyscarney f07a5b1
fix(segment-view): split opacity by mode vars to match segment
brandyscarney b75650b
fix(segment-view): don't query for disabled contents
brandyscarney 094d9a8
test(segment-view): remove toolbars
brandyscarney 522cbc6
test(segment-view): add tests for disabled content scrolling
brandyscarney 15b8b8f
chore(): add updated snapshots
brandyscarney 798e725
test: remove only
brandyscarney d811221
feat(segment): move indicator with scroll
brandyscarney 699ce97
fix(segment): properly move the indicator when direction starts out o…
brandyscarney 1d645c9
style: lint
brandyscarney c7bae07
fix(segment-view): allow moving the indicator left on scroll without …
brandyscarney 7fe1c09
fix(segment-view): always check the scrollLeft against the initial to…
brandyscarney bdc6933
fix(segment): properly bound indicator transform for more than 2 cont…
brandyscarney cbee05e
fix(segment): move indicator as a percentage of the width on scroll
brandyscarney 0a13ab4
fix(segment): clear transform styles on scroll end
0fa5c99
fix(segment): handle change of direction scrolling
16c728b
fix(segment): don't trigger scroll listener on segment button click
ae2704f
fix(segment): only handle events for correct instance
89d8e90
fix(segment): scroll segment button into view if appropriate
c6ec156
chore: build
brandyscarney e654743
style: naming
brandyscarney 641dc0b
refactor(segment-content): use opacity for disabled content
brandyscarney 59e306b
test(segment-view): remove not disabled styles
brandyscarney 279300f
fix(segment): update segment view to scroll past disabled content
brandyscarney b94bec2
fix(segment): update segment content to disabled when button is
brandyscarney 475de8b
fix(segment-view): continue to search through segment contents for en…
brandyscarney 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
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
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,10 @@ | ||
| // Segment Content | ||
| // -------------------------------------------------- | ||
|
|
||
| :host { | ||
| scroll-snap-align: center; | ||
|
|
||
| flex-shrink: 0; | ||
|
|
||
| width: 100%; | ||
| } |
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,17 @@ | ||
| import type { ComponentInterface } from '@stencil/core'; | ||
| import { Component, Host, h } from '@stencil/core'; | ||
|
|
||
| @Component({ | ||
| tag: 'ion-segment-content', | ||
| styleUrl: 'segment-content.scss', | ||
| shadow: true, | ||
| }) | ||
| export class SegmentContent implements ComponentInterface { | ||
| render() { | ||
| return ( | ||
| <Host> | ||
| <slot></slot> | ||
| </Host> | ||
| ); | ||
| } | ||
| } |
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,31 @@ | ||
| // Segment View | ||
| // -------------------------------------------------- | ||
|
|
||
| :host { | ||
| display: flex; | ||
|
|
||
| height: 100%; | ||
|
|
||
| overflow-x: scroll; | ||
| scroll-snap-type: x mandatory; | ||
|
|
||
| /* Hide scrollbar in Firefox */ | ||
| scrollbar-width: none; | ||
|
|
||
| /* Hide scrollbar in IE and Edge */ | ||
| -ms-overflow-style: none; | ||
| } | ||
|
|
||
| /* Hide scrollbar in webkit */ | ||
| :host::-webkit-scrollbar { | ||
| display: none; | ||
| } | ||
|
|
||
| :host(.segment-view-disabled) { | ||
| scroll-snap-type: none; | ||
|
|
||
| opacity: 0.7; | ||
|
|
||
| touch-action: none; | ||
| overflow-x: hidden; | ||
| } | ||
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,86 @@ | ||
| import type { ComponentInterface } from '@stencil/core'; | ||
| import { Component, Element, Host, Listen, Method, Prop, h } from '@stencil/core'; | ||
|
|
||
| @Component({ | ||
| tag: 'ion-segment-view', | ||
| styleUrl: 'segment-view.scss', | ||
| shadow: true, | ||
| }) | ||
| export class SegmentView implements ComponentInterface { | ||
| @Element() el!: HTMLElement; | ||
|
|
||
| /** | ||
| * If `true`, the segment view cannot be interacted with. | ||
| */ | ||
| @Prop() disabled = false; | ||
|
|
||
| @Listen('scroll') | ||
| handleScroll(ev: any) { | ||
| const { scrollLeft, offsetWidth } = ev.target; | ||
| const atSnappingPoint = scrollLeft % offsetWidth === 0; | ||
|
|
||
| if (!atSnappingPoint) return; | ||
|
|
||
| const index = Math.round(scrollLeft / offsetWidth); | ||
| const segmentContent = this.getSegmentContents()[index]; | ||
|
|
||
| if (segmentContent === null || segmentContent === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| const segmentButton = this.getSegmentButtonById(segmentContent.id) as HTMLIonSegmentButtonElement; | ||
| const segment = this.getParentSegment(segmentButton); | ||
|
|
||
| if (segment) { | ||
| segment.value = segmentButton.value; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This method is used to programmatically set the displayed segment content | ||
| * in the segment view. Calling this method will update the `value` of the | ||
| * corresponding segment button. | ||
| * @param id: The id of the segment content to display. | ||
| * @param smoothScroll: Whether to animate the scroll transition. | ||
| */ | ||
| @Method() | ||
| async setContent(id: string, smoothScroll = true) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be an internal method if we want, but I added an example using it because it seems useful to expose it. |
||
| const contents = this.getSegmentContents(); | ||
| const index = contents.findIndex((content) => content.id === id); | ||
|
|
||
| if (index === -1) return; | ||
|
|
||
| const contentWidth = this.el.offsetWidth; | ||
| this.el.scrollTo({ | ||
| top: 0, | ||
| left: index * contentWidth, | ||
| behavior: smoothScroll ? 'smooth' : 'auto', | ||
| }); | ||
| } | ||
|
|
||
| private getSegmentContents(): HTMLIonSegmentContentElement[] { | ||
| return Array.from(this.el.querySelectorAll('ion-segment-content')); | ||
| } | ||
|
|
||
| private getSegmentButtonById(id: string) { | ||
| return document.querySelector(`ion-segment-button[content-id="${id}"]`); | ||
| } | ||
|
|
||
| private getParentSegment(button: Element) { | ||
| return button.closest('ion-segment'); | ||
| } | ||
|
|
||
| render() { | ||
| const { disabled } = this; | ||
|
|
||
| return ( | ||
| <Host | ||
| class={{ | ||
| 'segment-view-disabled': disabled, | ||
| }} | ||
| > | ||
| <slot></slot> | ||
| </Host> | ||
| ); | ||
| } | ||
| } | ||
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.