Skip to content

Commit 722b585

Browse files
authored
typescript! allow any valid img prop in images list (#24)
* first pass on typescript, fix some issues * ⬆️ react-use-gesture 9.0.1 * ⬆️ typescript 4.1.4 * add some time buffer when switching between dragging/panning states, add some displayNames * fix some types * ⬆️ [email protected] * drop prop-types peer dep * add lint stage to travis * move test files to typescript, add @types/jest, drop unused fields from images type * update travis * add build:types command * setup jest for typescript * fix tests * add some test ids for rtl * update travis config * write build script and config to generate type def * comments * fix ts error, use touches param from Image onDrag * move prop comments to ts types * only emit declaration files when building types * fix linting, use yarn in travis * oops * fix install script * ⬆️ [email protected] * ⬆️ @babel/[email protected], @babel/[email protected] * ⬆️ [email protected] * ⬆️ example/[email protected], example/[email protected] * fix paging if holding a drag * cleanup timer in useDoubleClick hook * fix active drag check, reset memo translateX if drag disabled * fix drag, drop memo * ⬆️ eslint@^7.20.0 * export "ImagesListType", allow any valid img attribute in images array * allow experimental chrome img attr 'loading' * update comment * ⬆️ [email protected] * ⬆️ @rollup/[email protected], @rollup/[email protected], @typescript-eslint/[email protected], @typescript-eslint/[email protected], [email protected] * add types field to packages.json * fix build, use babel instead of typescript rollup plugin * add browserslist file
1 parent a49d504 commit 722b585

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1651
-1159
lines changed

.babelrc.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
module.exports = {
2-
presets: ['@babel/env', '@babel/react'],
2+
presets: ['@babel/env', '@babel/react', '@babel/preset-typescript'],
33
plugins: [
4-
['babel-plugin-styled-components'],
54
['@babel/plugin-proposal-class-properties'],
65
['@babel/plugin-proposal-object-rest-spread'],
7-
['transform-react-remove-prop-types', { removeImport: true }],
86
['@babel/plugin-transform-runtime', { regenerator: false }],
97
],
108
};

.browserslistrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> 0.5%, last 2 versions, Firefox ESR, not dead

.eslintrc.js

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,80 @@
1+
/* eslint-disable sort-keys */
12
/**
23
* Configure ESLint
34
*
45
* https://eslint.org/docs/user-guide/configuring
56
*/
67
module.exports = {
7-
parser: 'babel-eslint',
88
env: {
99
browser: true,
1010
es6: true,
1111
jest: true,
1212
},
13-
extends: ['airbnb', 'prettier', 'prettier/react', 'plugin:import/warnings'],
14-
plugins: ['prettier', 'jsx-a11y', 'react-hooks', 'import'],
13+
extends: [
14+
'plugin:react/recommended',
15+
'plugin:import/warnings',
16+
'plugin:@typescript-eslint/recommended',
17+
'plugin:prettier/recommended',
18+
'prettier/@typescript-eslint',
19+
],
1520
globals: {
1621
document: true,
1722
window: true,
1823
},
24+
parser: '@typescript-eslint/parser',
1925
parserOptions: {
2026
sourceType: 'module',
2127
},
28+
plugins: [
29+
'prettier',
30+
'jsx-a11y',
31+
'react',
32+
'react-hooks',
33+
'import',
34+
'sort-destructure-keys',
35+
'@typescript-eslint',
36+
],
37+
root: true,
2238
rules: {
23-
'react/forbid-prop-types': 0,
24-
'react/jsx-filename-extension': 0,
25-
'react/react-in-jsx-scope': 0,
26-
'class-methods-use-this': 0,
27-
'no-unused-expressions': ['error', { allowTaggedTemplates: true }],
28-
'react/no-unused-prop-types': 0,
29-
'consistent-return': 0,
30-
'jsx-a11y/anchor-is-valid': 0,
31-
'prettier/prettier': 'error',
32-
'react/destructuring-assignment': 0,
33-
'react/static-property-placement': 0,
34-
'react/jsx-props-no-spreading': 0,
3539
// Enforce React Hooks rules
3640
// https://www.npmjs.com/package/eslint-plugin-react-hooks
3741
'react-hooks/rules-of-hooks': 'error',
3842
'react-hooks/exhaustive-deps': 'warn',
43+
44+
'sort-destructure-keys/sort-destructure-keys': [
45+
'error',
46+
{ caseSensitive: false },
47+
],
48+
'sort-keys': ['error', 'asc', { caseSensitive: false, natural: false }],
49+
'sort-vars': [
50+
'error',
51+
{
52+
ignoreCase: true,
53+
},
54+
],
55+
'react/jsx-sort-props': ['error', { ignoreCase: true }],
56+
'@typescript-eslint/ban-ts-comment': 'off',
57+
'@typescript-eslint/no-explicit-any': 'off',
58+
'@typescript-eslint/explicit-module-boundary-types': 'off',
59+
'@typescript-eslint/member-ordering': [
60+
'error',
61+
{
62+
default: {
63+
order: 'alphabetically',
64+
},
65+
classes: {
66+
order: 'as-written',
67+
},
68+
},
69+
],
70+
},
71+
settings: {
72+
'import/resolver': {
73+
node: true,
74+
'eslint-import-resolver-typescript': true,
75+
},
76+
react: {
77+
version: 'detect',
78+
},
3979
},
4080
};

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ coverage/**
55
example/node_modules/**
66
example/.next/**
77
yarn.lock
8+
yarn-error.log
89
.editorconfig
910
.eslintignore
1011
.gitignore

.travis.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
language: node_js
22
node_js:
3-
- 'node'
43
- 'lts/*'
54
cache:
5+
yarn: true
66
directories:
7-
- ~/.npm
8-
install: npm install
9-
script:
10-
- npm run test
11-
- npm run build
7+
- node_modules
8+
install: yarn install
9+
jobs:
10+
include:
11+
- stage: lint
12+
script: yarn lint
13+
- stage: test
14+
script: yarn test
15+
- stage: build
16+
script: yarn build
1217
branches:
1318
only: master
1419
notifications:

example/components/GalleryLightbox/components/GridImage.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
import * as React from 'react';
12
import PropTypes from 'prop-types';
23
import styled from 'styled-components';
34

45
/**
56
* A single image element in a masonry style image grid
67
*/
7-
const GridImage = ({ key, index, left, top, photo, onClick }) => {
8-
const { height, width, src, alt, caption } = photo;
8+
const GridImage = ({ index, key, left, onClick, photo, top }) => {
9+
const { alt, caption, height, src, width } = photo;
910
return (
1011
<ImageContainer
11-
key={`${key}-${index}`}
1212
index={index}
13+
key={`${key}-${index}`}
1314
onClick={(e) => onClick(e, { index })}
14-
style={{ left, top, height, width }}
15+
style={{ height, left, top, width }}
1516
>
1617
<OverlayContainer>
17-
<Image src={src} alt={alt} caption={caption} />
18+
<Image alt={alt} caption={caption} src={src} />
1819
<Caption>
1920
<h4>{caption}</h4>
2021
</Caption>
@@ -24,19 +25,19 @@ const GridImage = ({ key, index, left, top, photo, onClick }) => {
2425
};
2526

2627
GridImage.propTypes = {
27-
key: PropTypes.string.isRequired,
28+
containerHeight: PropTypes.number.isRequired,
2829
index: PropTypes.number.isRequired,
30+
key: PropTypes.string.isRequired,
2931
left: PropTypes.number.isRequired,
30-
top: PropTypes.number.isRequired,
31-
containerHeight: PropTypes.number.isRequired,
3232
onClick: PropTypes.func.isRequired,
3333
photo: PropTypes.shape({
3434
alt: PropTypes.string.isRequired,
3535
caption: PropTypes.string.isRequired,
3636
height: PropTypes.number.isRequired,
37-
width: PropTypes.number.isRequired,
3837
src: PropTypes.string.isRequired,
38+
width: PropTypes.number.isRequired,
3939
}).isRequired,
40+
top: PropTypes.number.isRequired,
4041
};
4142

4243
export default GridImage;

example/components/GalleryLightbox/components/LightboxArrowButton.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* eslint-disable no-shadow */
2+
import * as React from 'react';
23
import PropTypes from 'prop-types';
34
import styled from 'styled-components';
45
import { IoIosArrowBack, IoIosArrowForward } from 'react-icons/io';
56
import { animated, useTransition } from '@react-spring/web';
67
import ButtonControl from './LightboxButtonControl';
78

8-
const ArrowButton = ({ position, onClick, disabled }) => {
9+
const ArrowButton = ({ disabled, onClick, position }) => {
910
const transitions = useTransition(!disabled, null, {
10-
from: { opacity: 0 },
1111
enter: { opacity: 1 },
12+
from: { opacity: 0 },
1213
leave: { opacity: 0 },
1314
});
1415

@@ -22,7 +23,7 @@ const ArrowButton = ({ position, onClick, disabled }) => {
2223
zIndex: 999,
2324
}}
2425
>
25-
<Button position={position} type="button" onClick={onClick}>
26+
<Button onClick={onClick} position={position} type="button">
2627
{position === 'left' && <IoIosArrowBack />}
2728
{position === 'right' && <IoIosArrowForward />}
2829
</Button>
@@ -32,9 +33,9 @@ const ArrowButton = ({ position, onClick, disabled }) => {
3233
};
3334

3435
ArrowButton.propTypes = {
35-
position: PropTypes.oneOf(['left', 'right']).isRequired,
36-
onClick: PropTypes.func.isRequired,
3736
disabled: PropTypes.bool,
37+
onClick: PropTypes.func.isRequired,
38+
position: PropTypes.oneOf(['left', 'right']).isRequired,
3839
};
3940

4041
ArrowButton.defaultProps = {

example/components/GalleryLightbox/components/LightboxHeader.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import * as React from 'react';
12
import PropTypes from 'prop-types';
23
import styled from 'styled-components';
34
import { IoIosClose } from 'react-icons/io';
45
import Color from 'color';
56
import ButtonControl from './LightboxButtonControl';
67

7-
const LightboxHeader = ({ galleryTitle, images, currentIndex, onClose }) => (
8+
const LightboxHeader = ({ currentIndex, galleryTitle, images, onClose }) => (
89
<TopHeaderBar>
910
<LeftSideDescriptionContainer>
1011
<GalleryHeading>{galleryTitle}</GalleryHeading>
@@ -25,18 +26,18 @@ const LightboxHeader = ({ galleryTitle, images, currentIndex, onClose }) => (
2526
);
2627

2728
LightboxHeader.propTypes = {
28-
onClose: PropTypes.func.isRequired,
29-
galleryTitle: PropTypes.string.isRequired,
3029
currentIndex: PropTypes.number.isRequired,
30+
galleryTitle: PropTypes.string.isRequired,
3131
images: PropTypes.arrayOf(
3232
PropTypes.shape({
33-
src: PropTypes.string.isRequired,
34-
caption: PropTypes.string.isRequired,
3533
alt: PropTypes.string.isRequired,
36-
width: PropTypes.number,
34+
caption: PropTypes.string.isRequired,
3735
height: PropTypes.number,
36+
src: PropTypes.string.isRequired,
37+
width: PropTypes.number,
3838
})
3939
).isRequired,
40+
onClose: PropTypes.func.isRequired,
4041
};
4142

4243
export default LightboxHeader;

example/components/GalleryLightbox/index.js

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class BlogImageGallery extends React.Component {
1515
imageMasonryDirection: PropTypes.oneOf(['column', 'row']),
1616
images: PropTypes.arrayOf(
1717
PropTypes.shape({
18-
src: PropTypes.string.isRequired,
19-
caption: PropTypes.string.isRequired,
2018
alt: PropTypes.string.isRequired,
21-
width: PropTypes.number,
19+
caption: PropTypes.string.isRequired,
2220
height: PropTypes.number,
21+
src: PropTypes.string.isRequired,
22+
width: PropTypes.number,
2323
})
2424
).isRequired,
2525
};
@@ -32,9 +32,9 @@ class BlogImageGallery extends React.Component {
3232
super();
3333

3434
this.state = {
35+
clientSide: false,
3536
currentImageIndex: 0,
3637
lightboxIsOpen: false,
37-
clientSide: false,
3838
};
3939
}
4040

@@ -93,52 +93,46 @@ class BlogImageGallery extends React.Component {
9393
};
9494

9595
render() {
96-
const { currentImageIndex, lightboxIsOpen, clientSide } = this.state;
97-
const { images, galleryTitle, imageMasonryDirection } = this.props;
96+
const { clientSide, currentImageIndex, lightboxIsOpen } = this.state;
97+
const { galleryTitle, imageMasonryDirection, images } = this.props;
98+
99+
// remove the height and width props for the lightbox images array
100+
const listboxImages = [...images].map((image) => {
101+
const newImage = { ...image };
102+
delete newImage.height;
103+
delete newImage.width;
104+
105+
return newImage;
106+
});
98107

99108
return (
100109
<GalleryContainer>
101110
{clientSide && (
102111
<Gallery
103112
columns={this.columnConfig}
113+
direction={imageMasonryDirection}
114+
margin={6}
104115
onClick={this.openLightbox}
105116
photos={images}
106-
margin={6}
107-
direction={imageMasonryDirection}
108117
renderImage={GridImage}
109118
/>
110119
)}
111120
<StyledLightbox
121+
currentIndex={currentImageIndex}
122+
galleryTitle={galleryTitle}
123+
images={listboxImages}
112124
isOpen={lightboxIsOpen}
113125
onClose={this.closeLightbox}
114-
onPrev={this.gotoPrevious}
115126
onNext={this.gotoNext}
116-
images={images}
117-
currentIndex={currentImageIndex}
118-
galleryTitle={galleryTitle}
119-
singleClickToZoom
127+
onPrev={this.gotoPrevious}
120128
renderHeader={() => (
121129
<LightboxHeader
130+
currentIndex={currentImageIndex}
122131
galleryTitle={galleryTitle}
123132
images={images}
124-
currentIndex={currentImageIndex}
125133
onClose={this.closeLightbox}
126134
/>
127135
)}
128-
renderPrevButton={({ canPrev }) => (
129-
<LightboxArrowButton
130-
position="left"
131-
onClick={this.gotoPrevious}
132-
disabled={!canPrev}
133-
/>
134-
)}
135-
renderNextButton={({ canNext }) => (
136-
<LightboxArrowButton
137-
position="right"
138-
onClick={this.gotoNext}
139-
disabled={!canNext}
140-
/>
141-
)}
142136
renderImageOverlay={() => (
143137
<ImageOverlay>
144138
<p>Create your own UI</p>
@@ -147,6 +141,21 @@ class BlogImageGallery extends React.Component {
147141
<FiHeart size="3em" />
148142
</ImageOverlay>
149143
)}
144+
renderNextButton={({ canNext }) => (
145+
<LightboxArrowButton
146+
disabled={!canNext}
147+
onClick={this.gotoNext}
148+
position="right"
149+
/>
150+
)}
151+
renderPrevButton={({ canPrev }) => (
152+
<LightboxArrowButton
153+
disabled={!canPrev}
154+
onClick={this.gotoPrevious}
155+
position="left"
156+
/>
157+
)}
158+
singleClickToZoom
150159
/>
151160
</GalleryContainer>
152161
);

0 commit comments

Comments
 (0)