1.3.3 • Published 4 years ago

react-new-window-styles v1.3.3

Weekly downloads
17
License
MIT
Repository
github
Last release
4 years ago

react-new-window-styles

This is a fork of react-new-window + styled-window-portal. Those 2 modules are really great when managing new windows, but those two together were lacking features! We needed a more composed solution, to handle both scenarios!

So i decided to merge those two together, where we can have styles from styled-components + styles from pure css added onto the page!

description

If a React portal is used to render to a new window, then styled-components will break as the styles declared are still being appended to the head of the original document. This package combats this by changing the inject point of the style to the head of the new window and copies globally injected styles to the new window.

We need a way to open new windows where we have general styles being added onto the page, without having so much pain!

Installation

yarn add -D react-new-window-styles
npm i -D react-new-window-styles

Example

import React from 'react';
import { render } from 'react-dom';
import styled, { createGlobalStyle } from 'styled-components';
import { ReactNewWindowStyles } from 'react-new-window-styles';

const MyDiv = styled.div`
  background: blue;
`;

const GlobalStyle = createGlobalStyle`
    body {
        margin: 0;
    }
`;

interface State {
  window: boolean;
}

class App extends React.Component<any, State> {
  constructor(props) {
    super(props);

    this.state = {
      window: false,
    };
  }

  render() {
    return (
      <div>
        <GlobalStyle />
        <button
          onClick={() =>
            this.setState({
              window: !this.state.window,
            })
          }
        >
          Click me to {this.state.window ? 'close' : 'open'} the window
        </button>
        {this.state.window && (
          <ReactNewWindowStyles
            onClose={() =>
              this.setState({
                window: false,
              })
            }
            autoClose
          >
            <MyDiv>Look, it&apos;s blue! There are no borders either.</MyDiv>
          </ReactNewWindowStyles>
        )}
      </div>
    );
  }
}
render(<App />, document.getElementById('app'));

Props

The styled window component can take the following props

Prop NameTypeDefault ValueDescription
onClosefunctionN/AA function called when the window is closed
autoClosebooleanfalseClose child window when parent is closed
titlestringNew WindowThe title of the window
namestring' 'The target attribute or the name of the window
windowPropsobjectSee below
copyStylesbooleanfalsecopy over styles from the main document to the new window

windowProps

All values here can either be constant, or functions, the functions have the other options passed to them as the first parameter, and the main window as the second parameter. See descriptions at https://www.w3schools.com/jsref/met_win_open.asp

Prop NameTypeDefault Value
toolbarboolean | functionfalse
locationboolean | functionfalse
directoriesboolean | functionfalse
menubarboolean | functionfalse
scrollbarsboolean | functiontrue
resizableboolean | functiontrue
widthnumber | function500
heightnumber | function400
topnumber | functionfunction (center)
leftnumber | functionfunction (center)