Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
feat(tab-scroller): Add scroll content width method for use in tab bar (
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickrodee authored Jul 26, 2018
1 parent c5caeba commit 052bc9e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/mdc-tab-scroller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ CSS Class | Description
`mdc-tab-scroller` | Mandatory. Contains the tab scroller content.
`mdc-tab-scroller__scroll-area` | Mandatory. Denotes the scrolling area.
`mdc-tab-scroller__scroll-content` | Mandatory. Denotes the scrolling content.
`mdc-tab-scroller--align-start` | Optional. Sets the elements inside the scroll content element to be aligned to the start of the scroll content element.
`mdc-tab-scroller--align-end` | Optional. Sets the elements inside the scroll content element to be aligned to the end of the scroll content element.
`mdc-tab-scroller--align-center` | Optional. Sets the elements inside the scroll content element to be aligned to the center of the scroll content element.

## `MDCTabScroller` Methods

Expand All @@ -73,6 +76,7 @@ Method Signature | Description
`scrollTo(scrollX: number) => void` | Scrolls to the scrollX value.
`incrementScroll(scrollX: number) => void` | Increments the current scroll value by the scrollX value.
`getScrollPosition() => number` | Returns the current visual scroll position.
`getScrollContentWidth() => number` | Returns the width of the scroll content element.

## Usage within Web Frameworks

Expand Down
5 changes: 5 additions & 0 deletions packages/mdc-tab-scroller/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ class MDCTabScrollerFoundation extends MDCFoundation {
* @param {number} scrollXIncrement The value by which to increment the scroll position
*/
incrementScroll(scrollXIncrement) {
// Early exit for non-operational increment values
if (scrollXIncrement === 0) {
return;
}

if (this.isRTL_()) {
return this.incrementScrollRTL_(scrollXIncrement);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/mdc-tab-scroller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ class MDCTabScroller extends MDCComponent {
return this.foundation_.getScrollPosition();
}

/**
* Returns the width of the scroll content
* @return {number}
*/
getScrollContentWidth() {
return this.content_.offsetWidth;
}

/**
* Increments the scroll value by the given amount
* @param {number} scrollXIncrement The pixel value by which to increment the scroll value
Expand Down
12 changes: 12 additions & 0 deletions packages/mdc-tab-scroller/mdc-tab-scroller.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@
will-change: transform;
}

.mdc-tab-scroller--align-start .mdc-tab-scroller__scroll-content {
justify-content: flex-start;
}

.mdc-tab-scroller--align-end .mdc-tab-scroller__scroll-content {
justify-content: flex-end;
}

.mdc-tab-scroller--align-center .mdc-tab-scroller__scroll-content {
justify-content: center;
}

.mdc-tab-scroller--animating .mdc-tab-scroller__scroll-area {
-webkit-overflow-scrolling: auto;
}
Expand Down
7 changes: 7 additions & 0 deletions test/unit/mdc-tab-scroller/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ test('#scrollTo() unsets the transform property in a rAF', () => {
td.verify(mockAdapter.setScrollContentStyleProperty('transform', 'none'), {times: 1});
});

test('#incrementScroll() exits early if increment is 0', () => {
const {foundation, mockAdapter} = setupScrollToTest({scrollLeft: 700});
foundation.incrementScroll(0);
td.verify(mockAdapter.setScrollContentStyleProperty(td.matchers.isA(String), td.matchers.isA(String)), {times: 0});
td.verify(mockAdapter.setScrollAreaScrollLeft(td.matchers.isA(Number)), {times: 0});
});

test('#incrementScroll() exits early if increment puts the scrollLeft over the max value', () => {
const {foundation, mockAdapter} = setupScrollToTest({scrollLeft: 700});
foundation.incrementScroll(10);
Expand Down
21 changes: 21 additions & 0 deletions test/unit/mdc-tab-scroller/mdc-tab-scroller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import bel from 'bel';
import {assert} from 'chai';
import td from 'testdouble';
import domEvents from 'dom-events';

import {
MDCTabScroller,
Expand Down Expand Up @@ -205,6 +206,12 @@ test('#getScrollPosition() calls getScrollPosition', () => {
td.verify(mockFoundation.getScrollPosition(), {times: 1});
});

test('#getScrollContentWidth() returns the offsetWidth of the content element', () => {
const {component, root} = setupMockFoundationTest();
const contentElement = root.querySelector(MDCTabScrollerFoundation.strings.CONTENT_SELECTOR);
assert.strictEqual(component.getScrollContentWidth(), contentElement.offsetWidth);
});

function setupTestRTL() {
const {root, content, component} = setupTest();
root.style.setProperty('width', '100px');
Expand All @@ -223,3 +230,17 @@ test('#getRTLScroller() returns an instance of MDCTabScrollerRTL', () => {
assert.instanceOf(component.getDefaultFoundation().getRTLScroller(), MDCTabScrollerRTL);
document.body.removeChild(root);
});

test('on interaction in the area element, call #handleInteraction()', () => {
const {root, mockFoundation} = setupMockFoundationTest();
const area = root.querySelector(MDCTabScrollerFoundation.strings.AREA_SELECTOR);
domEvents.emit(area, 'touchstart', {bubbles: true});
td.verify(mockFoundation.handleInteraction());
});

test('on transitionend of the content element, call #handleTransitionEnd()', () => {
const {root, mockFoundation} = setupMockFoundationTest();
const content = root.querySelector(MDCTabScrollerFoundation.strings.CONTENT_SELECTOR);
domEvents.emit(content, 'transitionend', {bubbles: true});
td.verify(mockFoundation.handleTransitionEnd(td.matchers.anything()));
});

0 comments on commit 052bc9e

Please sign in to comment.