Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The `children` contents is what will be rendered into the new popup window. In t
| `features` | `Object` | `{}` | The set of window features ([more details on `windowFeatures`](https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Window_features)). |
| `onUnload` | `Function` | `undefined` | A function to be triggered before the new window unload. |
| `onBlock` | `Function` | `undefined` | A function to be triggered when the new window could not be opened. |
| `center` | `String` | `parent` | Indicate how to center the new window. Valid values are: `parent` or `screen`. `parent` will center the new window according to its _parent_ window. `screen` will center the new window according to the _screen_. |
| `center` | `String` | `undefined` | Indicate how to center the new window. Valid values are: `parent` or `screen`. `parent` will center the new window according to its _parent_ window. `screen` will center the new window according to the _screen_. |
| `copyStyles` | `Boolean` | `true` | If specified, copy styles from parent window's document. |

## Tests
Expand Down
16 changes: 4 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 34 additions & 32 deletions src/NewWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class NewWindow extends React.PureComponent {
onBlock: null,
onOpen: null,
onUnload: null,
center: 'parent',
Copy link
Owner

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.

copyStyles: true
}

Expand Down Expand Up @@ -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.'
Copy link
Owner

Choose a reason for hiding this comment

The 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.
Expand Down
2 changes: 1 addition & 1 deletion stories/default.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DefaultStory extends React.PureComponent {
{opened && (
<NewWindow
onUnload={() => this.newWindowUnloaded()}
features={{ left: 200, top: 200, width: 400, height: 400 }}
features={{ width: 400, height: 400 }}
>
<h2>Hi 👋</h2>
<h4>Counting here as well {count}...</h4>
Expand Down
1 change: 0 additions & 1 deletion stories/features-prop.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class FeaturesPropStory extends React.PureComponent {
</Button>
{opened && (
<NewWindow
center={false}
features={features}
onUnload={() => this.newWindowUnloaded()}
>
Expand Down
62 changes: 62 additions & 0 deletions stories/position-centered-to-parent.stories.js
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 />)
62 changes: 62 additions & 0 deletions stories/position-centered-to-screen.stories.js
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 />)
10 changes: 9 additions & 1 deletion types/NewWindow.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems valid window feature are limited to: popup, width, height, left, top, noopener and noreferrer. The others are no longer working in modern browser.

[i: string]: boolean | number | string
}

Expand Down Expand Up @@ -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.
Expand Down