-
Notifications
You must be signed in to change notification settings - Fork 97
[WIP] feat(scrollbars): introduce the @zendeskgarden/react-scrollbars package #163
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
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,34 @@ | ||
| # @zendeskgarden/react-scrollbars [](https://www.npmjs.com/package/@zendeskgarden/react-scrollbars) | ||
|
|
||
| This package includes components relating to scrollbars in the | ||
| [Garden Design System](https://zendeskgarden.github.io/). | ||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| npm install @zendeskgarden/react-scrollbars | ||
|
|
||
| # Peer Dependencies - Also Required | ||
| npm install react react-dom prop-types styled-components @zendeskgarden/react-theming | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```jsx static | ||
| /** | ||
| * Include scrollbars styling at the root of your application | ||
| */ | ||
| import '@zendeskgarden/react-scrollbars/dist/styles.css'; | ||
|
|
||
| import { ThemeProvider } from '@zendeskgarden/react-theming'; | ||
| import { Scrollbar } from '@zendeskgarden/react-scrollbars'; | ||
|
|
||
| /** | ||
| * Place a `ThemeProvider` at the root of your React application | ||
| */ | ||
| <ThemeProvider> | ||
| <Scrollbar style={{ width: 200, height: 200 }}> | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit... | ||
| </Scrollbar> | ||
| </ThemeProvider>; | ||
| ``` |
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 @@ | ||
| { | ||
| "name": "@zendeskgarden/react-scrollbars", | ||
| "description": "Components relating to scrollbars in the Garden Design System", | ||
| "license": "Apache-2.0", | ||
| "author": "Zendesk Garden <garden@zendesk.com>", | ||
| "homepage": "https://garden.zendesk.com/", | ||
| "repository": "https://github.com/zendeskgarden/react-components", | ||
| "bugs": { | ||
| "url": "https://github.com/zendeskgarden/react-components/issues" | ||
| }, | ||
| "version": "0.0.0", | ||
| "main": "./dist/index.js", | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "scripts": { | ||
| "build": "../../utils/scripts/build.sh", | ||
| "build:demo": "../../utils/scripts/build-demo.sh", | ||
| "start": "../../utils/scripts/start.sh" | ||
| }, | ||
| "dependencies": { | ||
| "classnames": "^2.2.5", | ||
| "perfect-scrollbar": "^1.4.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "@zendeskgarden/react-theming": "^1.0.0 || ^2.0.0 || ^3.0.0", | ||
| "prop-types": "^15.6.1", | ||
| "react": "^0.14.0 || ^15.0.0 || ^16.0.0", | ||
| "react-dom": "^0.14.0 || ^15.0.0 || ^16.0.0", | ||
| "styled-components": "^3.2.6" | ||
| }, | ||
| "devDependencies": { | ||
| "@zendeskgarden/css-scrollbars": "0.1.0", | ||
| "@zendeskgarden/css-variables": "5.1.0" | ||
| }, | ||
| "keywords": [ | ||
| "components", | ||
| "garden", | ||
| "react", | ||
| "zendesk" | ||
| ], | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "zendeskgarden:library": "GardenScrollbars", | ||
| "zendeskgarden:src": "src/index.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,115 @@ | ||
| ### Basic Scrollable Example | ||
|
|
||
| Our custom scrollbar implementation is uses the | ||
| [perfect-scrollbars](https://github.com/utatti/perfect-scrollbar) | ||
| library internally. | ||
|
|
||
| Take a look at their [API documentation](https://github.com/utatti/perfect-scrollbar#options) | ||
| to view all of the available customization options and events. | ||
|
|
||
| ```jsx | ||
| const { MD } = require('@zendeskgarden/react-typography'); | ||
| const { zdSpacing } = require('@zendeskgarden/css-variables'); | ||
|
|
||
| const StyledSection = styled(MD)` | ||
| margin-bottom: ${zdSpacing}; | ||
| `; | ||
|
|
||
| const ExampleText = () => ( | ||
| <StyledSection> | ||
| Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque | ||
| laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto | ||
| beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut | ||
| odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. | ||
| Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, | ||
| sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat | ||
| voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit | ||
| laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui | ||
| in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat | ||
| quo voluptas nulla pariatur? | ||
| </StyledSection> | ||
| ); | ||
|
|
||
| <Scrollbar style={{ height: 200 }}> | ||
| <ExampleText /> | ||
| <ExampleText /> | ||
| <ExampleText /> | ||
| <ExampleText /> | ||
| <ExampleText /> | ||
| <ExampleText /> | ||
| </Scrollbar>; | ||
| ``` | ||
|
|
||
| ### Dark Mode | ||
|
|
||
| ```jsx | ||
| <Scrollbar style={{ width: '100%', height: 500 }} dark> | ||
| <img | ||
| src="images/beach-dark.jpg" | ||
| alt="Dark Beach Example" | ||
| style={{ width: 2000, display: 'block' }} | ||
| /> | ||
| </Scrollbar> | ||
| ``` | ||
|
|
||
| ## Scroll Jumping | ||
|
|
||
| ```jsx | ||
| const { Button } = require('@zendeskgarden/react-buttons/src'); | ||
|
|
||
| const StyledSection = styled.div` | ||
| margin-bottom: 16px; | ||
| `; | ||
|
|
||
| const ExampleText = () => ( | ||
| <StyledSection> | ||
| Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque | ||
| laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto | ||
| beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut | ||
| odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. | ||
| Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, | ||
| sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat | ||
| voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit | ||
| laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui | ||
| in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat | ||
| quo voluptas nulla pariatur? | ||
| </StyledSection> | ||
| ); | ||
|
|
||
| <Grid> | ||
| <Row> | ||
| <Col sm> | ||
| <Button | ||
| stretched | ||
| onClick={() => { | ||
| this.scrollbarRef.scrollTop = 0; | ||
| }} | ||
| > | ||
| Scroll to top | ||
| </Button> | ||
| </Col> | ||
| <Col sm> | ||
| <Button | ||
| stretched | ||
| onClick={() => { | ||
| this.scrollbarRef.scrollTop = this.scrollbarRef.scrollHeight; | ||
| }} | ||
| > | ||
| Scroll to bottom | ||
| </Button> | ||
| </Col> | ||
| </Row> | ||
| <Row> | ||
| <Col> | ||
| <Scrollbar style={{ height: 250, marginTop: 40 }} innerRef={ref => (this.scrollbarRef = ref)}> | ||
| <ExampleText /> | ||
| <ExampleText /> | ||
| <ExampleText /> | ||
| <ExampleText /> | ||
| <ExampleText /> | ||
| <ExampleText /> | ||
| </Scrollbar> | ||
| </Col> | ||
| </Row> | ||
| </Grid>; | ||
| ``` |
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,165 @@ | ||
| /** | ||
| * Copyright Zendesk, Inc. | ||
| * | ||
| * Use of this source code is governed under the Apache License, Version 2.0 | ||
| * found at http://www.apache.org/licenses/LICENSE-2.0. | ||
| */ | ||
|
|
||
| import React, { Component } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import classNames from 'classnames'; | ||
| import styled from 'styled-components'; | ||
| import PerfectScrollbar from 'perfect-scrollbar'; | ||
| import { isRtl, retrieveTheme } from '@zendeskgarden/react-theming'; | ||
|
|
||
| /** | ||
| * These styles are NOT consumed through CSS Modules to allow | ||
| * interaction with with perfect-scrollbars. | ||
| */ | ||
| import '@zendeskgarden/css-scrollbars'; | ||
|
|
||
| const EVENT_KEYS = { | ||
| SCROLL_Y: 'ps-scroll-y', | ||
| SCROLL_X: 'ps-scroll-x', | ||
| SCROLL_UP: 'ps-scroll-up', | ||
| SCROLL_DOWN: 'ps-scroll-down', | ||
| SCROLL_LEFT: 'ps-scroll-left', | ||
| SCROLL_RIGHT: 'ps-scroll-right', | ||
| REACH_START_Y: 'ps-y-reach-start', | ||
| REACH_END_Y: 'ps-y-reach-end', | ||
| REACH_START_X: 'ps-x-reach-start', | ||
| REACH_END_X: 'ps-x-reach-end' | ||
| }; | ||
|
|
||
| const COMPONENT_ID = 'scrollbars.scrollbar'; | ||
|
|
||
| /** | ||
| * Accepts all `<div>` props | ||
| */ | ||
| const ScrollbarContainer = styled.div.attrs({ | ||
| 'data-garden-id': COMPONENT_ID, | ||
| 'data-garden-version': PACKAGE_VERSION, | ||
| className: props => | ||
| classNames('c-scrollbar', { | ||
| 'c-scrollbar--dark': props.dark, | ||
| 'is-rtl': isRtl(props) | ||
| }) | ||
| })` | ||
| ${props => retrieveTheme(COMPONENT_ID, props)}; | ||
| `; | ||
|
|
||
| export default class Scrollbar extends Component { | ||
| static propTypes = { | ||
| /** Apply dark mode styling */ | ||
| dark: PropTypes.bool, | ||
| /** Customizations provided to [perfect-scrollbars](https://github.com/utatti/perfect-scrollbar) */ | ||
| perfectScrollbarOptions: PropTypes.object, | ||
| /** Container ref */ | ||
| innerRef: PropTypes.func, | ||
| /** Custom tag for the containing element */ | ||
| tagName: PropTypes.any, | ||
| onScrollY: PropTypes.func, | ||
| onScrollX: PropTypes.func, | ||
| onScrollUp: PropTypes.func, | ||
| onScrollDown: PropTypes.func, | ||
| onScrollLeft: PropTypes.func, | ||
| onScrollRight: PropTypes.func, | ||
| onReachStartY: PropTypes.func, | ||
| onReachEndY: PropTypes.func, | ||
| onReachStartX: PropTypes.func, | ||
| onReachEndX: PropTypes.func | ||
| }; | ||
|
|
||
| static defaultProps = { | ||
| tagName: 'div', | ||
| dark: false | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
| const { perfectScrollbarOptions } = this.props; | ||
|
|
||
| if (this.scrollbarContainerRef) { | ||
| this.perfectScrollbar = new PerfectScrollbar( | ||
| this.scrollbarContainerRef, | ||
| perfectScrollbarOptions | ||
| ); | ||
|
|
||
| for (const eventKey in EVENT_KEYS) { | ||
| if (Object.prototype.hasOwnProperty.call(EVENT_KEYS, eventKey)) { | ||
| this.scrollbarContainerRef.addEventListener( | ||
| EVENT_KEYS[eventKey], | ||
| this.onCustomEvent(EVENT_KEYS[eventKey]) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| componentDidUpdate() { | ||
| if (this.perfectScrollbar) { | ||
| this.perfectScrollbar.update(); | ||
| } | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| this.perfectScrollbar.destroy(); | ||
| this.perfectScrollbar = null; | ||
|
|
||
| for (const eventKey in EVENT_KEYS) { | ||
| if (Object.prototype.hasOwnProperty.call(EVENT_KEYS, eventKey)) { | ||
| this.scrollbarContainerRef.removeEventListener( | ||
| EVENT_KEYS[eventKey], | ||
| this.onCustomEvent(EVENT_KEYS[eventKey]) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| onCustomEvent = eventKey => { | ||
| const { | ||
| onScrollY, | ||
| onScrollX, | ||
| onScrollUp, | ||
| onScrollDown, | ||
| onScrollLeft, | ||
| onScrollRight, | ||
| onReachStartY, | ||
| onReachEndY, | ||
| onReachStartX, | ||
| onReachEndX | ||
| } = this.props; | ||
|
|
||
| const EVENTS = { | ||
| [EVENT_KEYS.SCROLL_Y]: onScrollY, | ||
| [EVENT_KEYS.SCROLL_X]: onScrollX, | ||
| [EVENT_KEYS.SCROLL_UP]: onScrollUp, | ||
| [EVENT_KEYS.SCROLL_DOWN]: onScrollDown, | ||
| [EVENT_KEYS.SCROLL_LEFT]: onScrollLeft, | ||
| [EVENT_KEYS.SCROLL_RIGHT]: onScrollRight, | ||
| [EVENT_KEYS.REACH_START_Y]: onReachStartY, | ||
| [EVENT_KEYS.REACH_END_Y]: onReachEndY, | ||
| [EVENT_KEYS.REACH_START_X]: onReachStartX, | ||
| [EVENT_KEYS.REACH_END_X]: onReachEndX | ||
| }; | ||
|
|
||
| return (...args) => EVENTS[eventKey] && EVENTS[eventKey](...args); | ||
| }; | ||
|
|
||
| render() { | ||
| const { children, innerRef, tagName, ...other } = this.props; | ||
|
|
||
| const CustomScrollbarContainer = ScrollbarContainer.withComponent(tagName); | ||
|
|
||
| return ( | ||
| <CustomScrollbarContainer | ||
| {...other} | ||
| innerRef={ref => { | ||
| this.scrollbarContainerRef = ref; | ||
| innerRef && innerRef(ref); | ||
| }} | ||
| > | ||
| {children} | ||
| </CustomScrollbarContainer> | ||
| ); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Horizontal scrolling for (not so)PerfectScrollbar is completely broken for RTL. Vertical isn't great, either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'm seeing some weird behaviour with rtl mode on