-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #1268 - New Component: BlockUI
- Loading branch information
1 parent
0061f5f
commit fc9f5a8
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,30 @@ | ||
.p-blockui-container { | ||
position: relative; | ||
} | ||
|
||
.p-blockui { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
background-color: transparent; | ||
opacity: 1; | ||
transition-property: background-color, opacity; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.p-blockui.p-component-overlay { | ||
position: absolute; | ||
} | ||
|
||
.p-blockui-document.p-component-overlay { | ||
position: fixed; | ||
} | ||
|
||
.p-blockui.p-blockui-leave.p-component-overlay { | ||
opacity: 0; | ||
background-color: transparent; | ||
} |
This file contains 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,18 @@ | ||
import * as React from 'react'; | ||
|
||
export interface BlockUIProps { | ||
id?: string; | ||
blocked?: boolean; | ||
fullScreen?: boolean; | ||
baseZIndex?: number; | ||
autoZIndex?: boolean; | ||
style?: object; | ||
className?: string; | ||
onBlocked?(): void; | ||
onUnblocked?(): void; | ||
} | ||
|
||
export declare class BlockUI extends React.Component<BlockUIProps, any> { | ||
public block(): void; | ||
public unblock(): void; | ||
} |
This file contains 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,130 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { classNames, DomHandler, ObjectUtils, ZIndexUtils } from '../utils/Utils'; | ||
import { Portal } from '../portal/Portal'; | ||
|
||
export class BlockUI extends Component { | ||
|
||
static defaultProps = { | ||
id: null, | ||
blocked: false, | ||
fullScreen: false, | ||
baseZIndex: 0, | ||
autoZIndex: true, | ||
style: null, | ||
className: null, | ||
template: null, | ||
onBlocked: null, | ||
onUnblocked: null | ||
}; | ||
|
||
static propTypes = { | ||
id: PropTypes.string, | ||
blocked: PropTypes.bool, | ||
fullScreen: PropTypes.bool, | ||
baseZIndex: PropTypes.number, | ||
autoZIndex: PropTypes.bool, | ||
style: PropTypes.object, | ||
className: PropTypes.string, | ||
template: PropTypes.any, | ||
onBlocked: PropTypes.func, | ||
onUnblocked: PropTypes.func | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
visible: props.blocked | ||
}; | ||
|
||
this.block = this.block.bind(this); | ||
this.unblock = this.unblock.bind(this); | ||
this.onPortalMounted = this.onPortalMounted.bind(this); | ||
} | ||
|
||
block() { | ||
this.setState({ visible: true }); | ||
} | ||
|
||
unblock() { | ||
DomHandler.addClass(this.mask, 'p-blockui-leave'); | ||
this.mask.addEventListener('transitionend', () => { | ||
ZIndexUtils.clear(this.mask); | ||
this.setState({ visible: false }, () => { | ||
this.props.fullScreen && DomHandler.removeClass(document.body, 'p-overflow-hidden'); | ||
this.props.onUnblocked && this.props.onUnblocked(); | ||
}); | ||
}); | ||
} | ||
|
||
onPortalMounted() { | ||
if (this.props.fullScreen) { | ||
DomHandler.addClass(document.body, 'p-overflow-hidden'); | ||
document.activeElement.blur(); | ||
} | ||
|
||
if (this.mask) { | ||
setTimeout(() => { | ||
DomHandler.addClass(this.mask, 'p-component-overlay'); | ||
}, 1); | ||
} | ||
|
||
if (this.props.autoZIndex) { | ||
ZIndexUtils.set(this.props.fullScreen ? 'modal' : 'overlay', this.mask, this.props.baseZIndex); | ||
} | ||
|
||
this.props.onBlocked && this.props.onBlocked(); | ||
} | ||
|
||
renderMask() { | ||
if (this.state.visible) { | ||
const className = classNames('p-blockui', { | ||
'p-blockui-document': this.props.fullScreen | ||
}, this.props.className); | ||
const content = this.props.template ? ObjectUtils.getJSXElement(this.props.template, this.props) : null; | ||
const mask = ( | ||
<div ref={(el) => this.mask = el} className={className} style={this.props.style}> | ||
{content} | ||
</div> | ||
); | ||
|
||
return ( | ||
<Portal element={mask} appendTo={this.props.fullScreen ? document.body : 'self'} onMounted={this.onPortalMounted} /> | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
componentDidMount() { | ||
if (this.state.visible) { | ||
this.block(); | ||
} | ||
} | ||
|
||
componentDidUpdate(prevProps, prevState) { | ||
if (prevProps.blocked !== this.props.blocked) { | ||
this.props.blocked ? this.block() : this.unblock(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.props.fullScreen) { | ||
DomHandler.removeClass(document.body, 'p-overflow-hidden'); | ||
} | ||
|
||
ZIndexUtils.clear(this.mask); | ||
} | ||
|
||
render() { | ||
const mask = this.renderMask(); | ||
|
||
return ( | ||
<div ref={(el) => this.container = el} id={this.props.id} className="p-blockui-container"> | ||
{this.props.children} | ||
{mask} | ||
</div> | ||
); | ||
} | ||
} |
This file contains 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