Skip to content

Commit e4b0e85

Browse files
committed
first commit
0 parents  commit e4b0e85

16 files changed

+13962
-0
lines changed

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "es5",
4+
"singleQuote": true,
5+
"printWidth": 70
6+
}

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: node_js
2+
3+
node_js:
4+
- stable
5+
6+
install:
7+
- npm install
8+
9+
script:
10+
- npm test

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# react-range
2+
3+
[![Build Status](https://travis-ci.org/the-road-to-learn-react/react-range.svg?branch=master)](https://travis-ci.org/the-road-to-learn-react/react-range) [![Slack](https://slack-the-road-to-learn-react.wieruch.com/badge.svg)](https://slack-the-road-to-learn-react.wieruch.com/) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-learn-react/react-range.svg)](https://greenkeeper.io/)
4+
5+
Showcasing how a React Range Component could be implemented in React. [Read more about it.](https://www.robinwieruch.de/react-range/)
6+
7+
[![Edit react-range](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/the-road-to-learn-react/react-range/tree/master/?fontsize=14)
8+
9+
## Installation
10+
11+
- `git clone git@github.com:the-road-to-learn-react/react-range.git`
12+
- cd react-range
13+
- npm install
14+
- npm start
15+
- visit `http://localhost:3000`

package-lock.json

Lines changed: 13533 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "react-slider",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"react": "^16.12.0",
7+
"react-dom": "^16.12.0",
8+
"react-scripts": "3.2.0",
9+
"styled-components": "^4.4.1"
10+
},
11+
"scripts": {
12+
"start": "react-scripts start",
13+
"build": "react-scripts build",
14+
"test": "react-scripts test",
15+
"eject": "react-scripts eject"
16+
},
17+
"eslintConfig": {
18+
"extends": "react-app"
19+
},
20+
"browserslist": {
21+
"production": [
22+
">0.2%",
23+
"not dead",
24+
"not op_mini all"
25+
],
26+
"development": [
27+
"last 1 chrome version",
28+
"last 1 firefox version",
29+
"last 1 safari version"
30+
]
31+
}
32+
}

public/favicon.ico

21.9 KB
Binary file not shown.

public/index.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>

public/logo192.png

8.38 KB
Loading

public/logo512.png

22.4 KB
Loading

public/manifest.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "logo192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "logo512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}

public/robots.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *

src/App.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import React from 'react';
2+
import styled from 'styled-components';
3+
4+
const RangeHeader = styled.div`
5+
display: flex;
6+
justify-content: flex-end;
7+
`;
8+
9+
const StyledRange = styled.div`
10+
border-radius: 3px;
11+
background: #dddddd;
12+
margin: 5px;
13+
height: 15px;
14+
`;
15+
16+
const StyledThumb = styled.div`
17+
width: 10px;
18+
height: 25px;
19+
border-radius: 3px;
20+
position: relative;
21+
top: -5px;
22+
opacity: 0.5;
23+
background: #823eb7;
24+
cursor: pointer;
25+
`;
26+
27+
const getPercentage = (current, max) => (100 * current) / max;
28+
29+
const getValue = (percentage, max) => (max / 100) * percentage;
30+
31+
const getLeft = percentage => `calc(${percentage}% - 5px)`;
32+
33+
const Range = ({
34+
initial,
35+
max,
36+
formatFn = number => number.toFixed(0),
37+
onChange,
38+
}) => {
39+
const initialPercentage = getPercentage(initial, max);
40+
41+
const rangeRef = React.useRef();
42+
const thumbRef = React.useRef();
43+
const currentRef = React.useRef();
44+
45+
const diff = React.useRef();
46+
47+
const handleMouseMove = event => {
48+
let newX =
49+
event.clientX -
50+
diff.current -
51+
rangeRef.current.getBoundingClientRect().left;
52+
53+
const end =
54+
rangeRef.current.offsetWidth - thumbRef.current.offsetWidth;
55+
56+
const start = 0;
57+
58+
if (newX < start) {
59+
newX = 0;
60+
}
61+
62+
if (newX > end) {
63+
newX = end;
64+
}
65+
66+
const newPercentage = getPercentage(newX, end);
67+
const newValue = getValue(newPercentage, max);
68+
69+
thumbRef.current.style.left = getLeft(newPercentage);
70+
currentRef.current.textContent = formatFn(newValue);
71+
72+
onChange(newValue);
73+
};
74+
75+
const handleMouseUp = () => {
76+
document.removeEventListener('mouseup', handleMouseUp);
77+
document.removeEventListener('mousemove', handleMouseMove);
78+
};
79+
80+
const handleMouseDown = event => {
81+
diff.current =
82+
event.clientX - thumbRef.current.getBoundingClientRect().left;
83+
84+
document.addEventListener('mousemove', handleMouseMove);
85+
document.addEventListener('mouseup', handleMouseUp);
86+
};
87+
88+
return (
89+
<>
90+
<RangeHeader>
91+
<strong ref={currentRef}>{formatFn(initial)}</strong>
92+
&nbsp;/&nbsp;
93+
{max}
94+
</RangeHeader>
95+
<StyledRange ref={rangeRef}>
96+
<StyledThumb
97+
style={{ left: getLeft(initialPercentage) }}
98+
ref={thumbRef}
99+
onMouseDown={handleMouseDown}
100+
/>
101+
</StyledRange>
102+
</>
103+
);
104+
};
105+
106+
const App = () => (
107+
<div>
108+
<Range
109+
initial={10}
110+
max={25}
111+
formatFn={number => number.toFixed(2)}
112+
onChange={value => console.log(value)}
113+
/>
114+
</div>
115+
);
116+
117+
export default App;

src/App.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
5+
it('renders without crashing', () => {
6+
const div = document.createElement('div');
7+
ReactDOM.render(<App />, div);
8+
ReactDOM.unmountComponentAtNode(div);
9+
});

src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
import App from './App';
5+
import * as serviceWorker from './serviceWorker';
6+
7+
ReactDOM.render(<App />, document.getElementById('root'));
8+
9+
// If you want your app to work offline and load faster, you can change
10+
// unregister() to register() below. Note this comes with some pitfalls.
11+
// Learn more about service workers: https://bit.ly/CRA-PWA
12+
serviceWorker.unregister();

0 commit comments

Comments
 (0)