Skip to content

Commit a74c4bc

Browse files
authored
Merge pull request #357 from hsource/dismiss-backdrop-click
Made clicking on more parts of the backdrop dismiss modal
2 parents c66321d + 2261155 commit a74c4bc

6 files changed

Lines changed: 52 additions & 11 deletions

File tree

src/components/Carousel.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { type ModalProps } from './Modal/Modal';
1818
import { className, isTouch } from '../utils';
1919
import formatters from '../formatters';
2020
import { type ViewsType } from '../types';
21+
import componentBaseClassNames from './componentBaseClassNames';
2122

2223
type SpringConfig = { [key: string]: number };
2324
export type fn = any => void;
@@ -87,6 +88,8 @@ const defaultProps = {
8788
},
8889
};
8990

91+
const trackBaseClassName = componentBaseClassNames.Track;
92+
9093
class Carousel extends Component<CarouselProps, CarouselState> {
9194
commonProps: any; // TODO
9295
components: CarouselComponents;
@@ -381,7 +384,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
381384
{...this.getTrackProps(this.props)}
382385
style={{ display: 'flex', alignItems: 'center' }}
383386
currentView={currentIndex}
384-
className={className('track')}
387+
className={className(trackBaseClassName)}
385388
onViewChange={this.handleViewChange}
386389
ref={this.getTrack}
387390
>

src/components/Footer.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { smallDevice } from './css-helpers';
77
import { Div, Span } from '../primitives';
88
import type { PropsWithStyles, ViewType } from '../types';
99
import { className } from '../utils';
10+
import componentBaseClassNames from './componentBaseClassNames';
1011

1112
type State = { isModal: boolean, interactionIsIdle: boolean };
1213
type Props = State &
@@ -42,6 +43,8 @@ export const footerCSS = ({ isModal, interactionIsIdle }: State) => ({
4243
},
4344
});
4445

46+
const footerBaseClassName = componentBaseClassNames.Footer;
47+
4548
const Footer = (props: Props) => {
4649
const { components, getStyles, innerProps, isFullscreen, isModal } = props;
4750

@@ -51,12 +54,12 @@ const Footer = (props: Props) => {
5154

5255
const state = { isFullscreen, isModal };
5356
const cn = {
54-
container: className('footer', state),
57+
container: className(footerBaseClassName, state),
5558
caption: className('footer__caption', state),
5659
count: className('footer__count', state),
5760
};
5861
const css = {
59-
container: getStyles('footer', props),
62+
container: getStyles(footerBaseClassName, props),
6063
caption: getStyles('footerCaption', props),
6164
count: getStyles('footerCount', props),
6265
};

src/components/Header.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Button, Div } from '../primitives';
77
import { className } from '../utils';
88
import type { PropsWithStyles } from '../types';
99
import { Close, FullscreenEnter, FullscreenExit } from './svg';
10+
import componentBaseClassNames from './componentBaseClassNames';
1011

1112
type State = { interactionIsIdle: boolean };
1213
type Props = PropsWithStyles &
@@ -36,6 +37,8 @@ export const headerCSS = ({ interactionIsIdle }: State) => ({
3637
zIndex: 1,
3738
});
3839

40+
const headerBaseClassName = componentBaseClassNames.Header;
41+
3942
const Header = (props: Props) => {
4043
const {
4144
components,
@@ -61,8 +64,8 @@ const Header = (props: Props) => {
6164

6265
return (
6366
<Div
64-
css={getStyles('header', props)}
65-
className={className('header', state)}
67+
css={getStyles(headerBaseClassName, props)}
68+
className={className(headerBaseClassName, state)}
6669
// TODO glam prefixer fails on gradients
6770
// https://github.com/threepointone/glam/issues/35
6871
style={{

src/components/Modal/Modal.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Fade, SlideUp } from './Animation';
1313
import { type CarouselType } from '../Carousel';
1414
import { defaultModalStyles, type ModalStylesConfig } from '../../styles';
1515
import { isTouch, className } from '../../utils';
16+
import componentBaseClassNames from '../componentBaseClassNames';
1617

1718
type MouseOrKeyboardEvent = MouseEvent | KeyboardEvent;
1819
export type CloseType = (event: MouseOrKeyboardEvent) => void;
@@ -55,6 +56,17 @@ const defaultProps = {
5556
preventScroll: true,
5657
styles: {},
5758
};
59+
60+
/** Classes that when clicked on, close the backdrop */
61+
const backdropClassNames = new Set(
62+
[
63+
componentBaseClassNames.View,
64+
componentBaseClassNames.Header,
65+
componentBaseClassNames.Footer,
66+
componentBaseClassNames.Track,
67+
].map(className),
68+
);
69+
5870
class Modal extends Component<Props, State> {
5971
commonProps: any; // TODO
6072
components: ModalComponents;
@@ -106,10 +118,14 @@ class Modal extends Component<Props, State> {
106118
if (allowClose) this.handleClose(event);
107119
};
108120
handleBackdropClick = (event: MouseEvent) => {
109-
if (
110-
!event.target.classList.contains(className('view')) ||
111-
!this.props.closeOnBackdropClick
112-
) {
121+
let hasBackdropClassName = false;
122+
for (const targetClass of event.target.classList) {
123+
if (backdropClassNames.has(targetClass)) {
124+
hasBackdropClassName = true;
125+
}
126+
}
127+
128+
if (!hasBackdropClassName || !this.props.closeOnBackdropClick) {
113129
return;
114130
}
115131

src/components/View.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Div, Img } from '../primitives';
77
import { type PropsWithStyles } from '../types';
88
import { className } from '../utils';
99
import { getSource } from './component-helpers';
10+
import componentBaseClassNames from './componentBaseClassNames';
1011

1112
type Props = PropsWithStyles & {
1213
data: Object,
@@ -20,6 +21,8 @@ export const viewCSS = () => ({
2021
textAlign: 'center',
2122
});
2223

24+
const viewBaseClassName = componentBaseClassNames.View;
25+
2326
const View = (props: Props) => {
2427
const { data, formatters, getStyles, index, isFullscreen, isModal } = props;
2528
const innerProps = {
@@ -29,8 +32,8 @@ const View = (props: Props) => {
2932

3033
return (
3134
<Div
32-
css={getStyles('view', props)}
33-
className={className('view', { isFullscreen, isModal })}
35+
css={getStyles(viewBaseClassName, props)}
36+
className={className(viewBaseClassName, { isFullscreen, isModal })}
3437
>
3538
<Img
3639
{...innerProps}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Used to get the HTML class to select specific components.
3+
* We call `className()` in utils with each of these to get the full className,
4+
* with prefixes.
5+
*/
6+
const componentBaseClassNames = {
7+
Header: 'header',
8+
Footer: 'footer',
9+
View: 'view',
10+
Track: 'track',
11+
};
12+
13+
export default componentBaseClassNames;

0 commit comments

Comments
 (0)