Skip to content

Commit 915238e

Browse files
committed
feat: improve <Dimmer> component
1 parent b3e1817 commit 915238e

File tree

2 files changed

+60
-44
lines changed

2 files changed

+60
-44
lines changed

src/Dimmer/__story__/story.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,53 @@ import {storiesOf} from '@storybook/react';
33
import {action} from '@storybook/addon-actions';
44
import {linkTo} from '@storybook/addon-links';
55
import {Dimmer} from '..';
6+
import {Toggle} from '../../Toggle';
67
import ShowDocs from '../../../.storybook/ShowDocs'
78

89
storiesOf('UI/Dimmer', module)
910
// .add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/Dimmer.md')}))
1011
.add('Example', () =>
1112
<div style={{width: 500, height: 300, border: '1px solid tomato', position: 'absolute'}}>
13+
Inline...
1214
<Dimmer>
1315
Children...
1416
</Dimmer>
1517
</div>
1618
)
19+
.add('Multiple sibling nodes', () =>
20+
<div style={{width: 500, height: 300, border: '1px solid tomato', position: 'absolute'}}>
21+
<div>
22+
Sibling 1
23+
</div>
24+
<div>
25+
Sibling 2
26+
</div>
27+
<Dimmer>
28+
Children...
29+
</Dimmer>
30+
<div>
31+
Sibling 3
32+
</div>
33+
</div>
34+
)
35+
.add('hidden=', () =>
36+
<div style={{width: 500, height: 300, border: '1px solid tomato', position: 'absolute'}}>
37+
Inline...
38+
<Dimmer hidden>
39+
Children...
40+
</Dimmer>
41+
</div>
42+
)
43+
.add('Toggle', () =>
44+
<Toggle>{({on, toggle}) =>
45+
<div>
46+
<div style={{width: 500, height: 300, border: '1px solid tomato', position: 'absolute'}}>
47+
Inline...
48+
<Dimmer hidden={!on}>
49+
Children...
50+
</Dimmer>
51+
</div>
52+
<div onClick={toggle}>Toggle: {on ? 'on' : 'off'}</div>
53+
</div>
54+
}</Toggle>
55+
)

src/Dimmer/index.tsx

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,23 @@ import {h} from '../util';
33
import {render} from 'react-universal-interface';
44

55
export interface IDimmerProps {
6-
blur?: number;
76
color?: string;
87
hidden?: boolean;
98
}
109

11-
export interface IDimmerState {
12-
}
13-
14-
export class Dimmer extends Component<IDimmerProps, IDimmerState> {
10+
export class Dimmer extends Component<IDimmerProps, {}> {
1511
static defaultProps = {
16-
blur: 5,
1712
color: 'rgba(0,0,0,0.5)',
1813
};
1914

2015
el: HTMLElement = null;
2116

22-
state: IDimmerState = {
23-
24-
};
25-
2617
ref = (el) => {
2718
this.el = el;
2819
};
2920

3021
componentDidMount () {
3122
const parent = this.el.parentElement;
32-
33-
this.applyDOMChanges();
34-
}
35-
36-
componentWillUnmount () {
37-
this.removeDOMChanges();
38-
}
39-
40-
applyDOMChanges () {
41-
const parent = this.el.parentElement;
42-
const siblings = Array.from(document.body.children);
4323
const position = parent.style.getPropertyValue('position');
4424

4525
if (process.env.NODE_ENV !== 'production') {
@@ -55,34 +35,31 @@ export class Dimmer extends Component<IDimmerProps, IDimmerState> {
5535
if (position !== 'relative') {
5636
parent.style.setProperty('position', 'relative', 'important');
5737
}
58-
59-
for (let i = 0; i < siblings.length; i++) {
60-
const sibling = siblings[i];
61-
62-
if (sibling === this.el) {
63-
continue;
64-
}
65-
66-
67-
}
68-
}
69-
70-
removeDOMChanges () {
71-
7238
}
7339

7440
render () {
41+
let style: any = {
42+
background: this.props.color,
43+
position: 'absolute',
44+
top: 0,
45+
left: 0,
46+
width: '100%',
47+
height: '100%',
48+
zIndex: 2147483647, // Max z-index.
49+
display: 'flex',
50+
justifyContent: 'center',
51+
alignItems: 'center',
52+
transition: 'opacity .3s',
53+
};
54+
55+
if (this.props.hidden) {
56+
style.opacity = 0;
57+
style.pointerEvents = 'none';
58+
}
59+
7560
return h('div', {
7661
ref: this.ref,
77-
style: {
78-
background: this.props.color,
79-
position: 'absolute',
80-
top: 0,
81-
left: 0,
82-
width: '100%',
83-
height: '100%',
84-
zIndex: 2147483647, // Max z-index.
85-
}
62+
style,
8663
},
8764
render(this.props, this.state)
8865
);

0 commit comments

Comments
 (0)