-
Notifications
You must be signed in to change notification settings - Fork 111
Disable default centering (#47) and improve typings #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ class NewWindow extends React.PureComponent { | |
onBlock: null, | ||
onOpen: null, | ||
onUnload: null, | ||
center: 'parent', | ||
copyStyles: true | ||
} | ||
|
||
|
@@ -63,37 +62,40 @@ class NewWindow extends React.PureComponent { | |
const { url, title, name, features, onBlock, onOpen, center } = this.props | ||
|
||
// Prepare position of the new window to be centered against the 'parent' window or 'screen'. | ||
if ( | ||
typeof center === 'string' && | ||
(features.width === undefined || features.height === undefined) | ||
) { | ||
console.warn( | ||
'width and height window features must be present when a center prop is provided' | ||
) | ||
} else if (center === 'parent') { | ||
features.left = | ||
window.top.outerWidth / 2 + window.top.screenX - features.width / 2 | ||
features.top = | ||
window.top.outerHeight / 2 + window.top.screenY - features.height / 2 | ||
} else if (center === 'screen') { | ||
const screenLeft = | ||
window.screenLeft !== undefined ? window.screenLeft : window.screen.left | ||
const screenTop = | ||
window.screenTop !== undefined ? window.screenTop : window.screen.top | ||
|
||
const width = window.innerWidth | ||
? window.innerWidth | ||
: document.documentElement.clientWidth | ||
? document.documentElement.clientWidth | ||
: window.screen.width | ||
const height = window.innerHeight | ||
? window.innerHeight | ||
: document.documentElement.clientHeight | ||
? document.documentElement.clientHeight | ||
: window.screen.height | ||
|
||
features.left = width / 2 - features.width / 2 + screenLeft | ||
features.top = height / 2 - features.height / 2 + screenTop | ||
if (typeof center === 'string') { | ||
if (features.width == null || features.height == null) { | ||
console.warn( | ||
'react-new-window: "width" and "height" window features must be present when a center prop is provided.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love that detail. |
||
) | ||
} else if (center === 'parent') { | ||
features.left = | ||
window.top.outerWidth / 2 + window.top.screenX - features.width / 2 | ||
features.top = | ||
window.top.outerHeight / 2 + window.top.screenY - features.height / 2 | ||
} else if (center === 'screen') { | ||
const screenLeft = | ||
window.screenLeft != null ? window.screenLeft : window.screen.left | ||
const screenTop = | ||
window.screenTop != null ? window.screenTop : window.screen.top | ||
|
||
const width = window.innerWidth | ||
? window.innerWidth | ||
: document.documentElement.clientWidth | ||
? document.documentElement.clientWidth | ||
: window.screen.width | ||
const height = window.innerHeight | ||
? window.innerHeight | ||
: document.documentElement.clientHeight | ||
? document.documentElement.clientHeight | ||
: window.screen.height | ||
|
||
features.left = width / 2 - features.width / 2 + screenLeft | ||
features.top = height / 2 - features.height / 2 + screenTop | ||
} else { | ||
console.warn( | ||
'react-new-window: Invalid `center` string provided. Only these strings are permitted: "parent", "screen".' | ||
) | ||
} | ||
} | ||
|
||
// Open a new window. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react' | ||
import { action } from '@storybook/addon-actions' | ||
import NewWindow from '../src/NewWindow' | ||
import Button from './components/Button' | ||
import Container from './components/Container' | ||
import { storiesOf } from '@storybook/react' | ||
|
||
const stories = storiesOf('react-new-window', module) | ||
|
||
class CenteredToParentStory extends React.PureComponent { | ||
state = { | ||
opened: false, | ||
count: 0 | ||
} | ||
|
||
componentDidMount() { | ||
this.interval = setInterval(() => { | ||
this.setState(prevState => ({ count: prevState.count + 1 })) | ||
}, 1000) | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.interval) | ||
} | ||
|
||
render() { | ||
const { opened, count } = this.state | ||
return ( | ||
<Container> | ||
<h2>React New Window</h2> | ||
<h3>prop: center: "parent"</h3> | ||
<h4>Counting {count}...</h4> | ||
<Button onClick={() => this.toggleOpened()}> | ||
{opened ? 'Close the opened window' : 'Open a new window'} | ||
</Button> | ||
{opened && ( | ||
<NewWindow | ||
onUnload={() => this.newWindowUnloaded()} | ||
center="parent" | ||
features={{ left: 200, top: 200, width: 400, height: 400 }} | ||
> | ||
<h2>Hi 👋</h2> | ||
<h4>Counting here as well {count}...</h4> | ||
<Button>Keeping the same style as my parent</Button> | ||
</NewWindow> | ||
)} | ||
</Container> | ||
) | ||
} | ||
|
||
toggleOpened() { | ||
action(this.state.opened ? 'Closing the window' : 'Opening the window')() | ||
this.setState(prevState => ({ opened: !prevState.opened })) | ||
} | ||
|
||
newWindowUnloaded() { | ||
action('Window unloaded')() | ||
this.setState({ opened: false }) | ||
} | ||
} | ||
|
||
stories.add('Position: Centered To Parent', () => <CenteredToParentStory />) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react' | ||
import { action } from '@storybook/addon-actions' | ||
import NewWindow from '../src/NewWindow' | ||
import Button from './components/Button' | ||
import Container from './components/Container' | ||
import { storiesOf } from '@storybook/react' | ||
|
||
const stories = storiesOf('react-new-window', module) | ||
|
||
class CenteredToScreenStory extends React.PureComponent { | ||
state = { | ||
opened: false, | ||
count: 0 | ||
} | ||
|
||
componentDidMount() { | ||
this.interval = setInterval(() => { | ||
this.setState(prevState => ({ count: prevState.count + 1 })) | ||
}, 1000) | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.interval) | ||
} | ||
|
||
render() { | ||
const { opened, count } = this.state | ||
return ( | ||
<Container> | ||
<h2>React New Window</h2> | ||
<h3>prop: center: "screen"</h3> | ||
<h4>Counting {count}...</h4> | ||
<Button onClick={() => this.toggleOpened()}> | ||
{opened ? 'Close the opened window' : 'Open a new window'} | ||
</Button> | ||
{opened && ( | ||
<NewWindow | ||
onUnload={() => this.newWindowUnloaded()} | ||
center="screen" | ||
features={{ left: 200, top: 200, width: 400, height: 400 }} | ||
> | ||
<h2>Hi 👋</h2> | ||
<h4>Counting here as well {count}...</h4> | ||
<Button>Keeping the same style as my parent</Button> | ||
</NewWindow> | ||
)} | ||
</Container> | ||
) | ||
} | ||
|
||
toggleOpened() { | ||
action(this.state.opened ? 'Closing the window' : 'Opening the window')() | ||
this.setState(prevState => ({ opened: !prevState.opened })) | ||
} | ||
|
||
newWindowUnloaded() { | ||
action('Window unloaded')() | ||
this.setState({ opened: false }) | ||
} | ||
} | ||
|
||
stories.add('Position: Centered To Screen', () => <CenteredToScreenStory />) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,14 @@ declare module 'react-new-window' { | |
export interface IWindowFeatures { | ||
height: number | ||
width: number | ||
left: number | ||
top: number | ||
menubar: boolean | ||
toolbar: boolean | ||
scrollbars: boolean | ||
location: boolean | ||
status: boolean | ||
resizable: boolean | ||
Comment on lines
+16
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems valid window feature are limited to: |
||
[i: string]: boolean | number | string | ||
} | ||
|
||
|
@@ -40,7 +48,7 @@ declare module 'react-new-window' { | |
/** | ||
* The set of window features. | ||
*/ | ||
features?: IWindowFeatures | ||
features?: Partial<IWindowFeatures> | ||
|
||
/** | ||
* A function to be triggered before the new window unload. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will change a default.