npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-openfin

v1.1.0

Published

React utils for openfin app development

Downloads

8

Readme

React Openfin

version license Build Status Coverage Status

React utils for Openfin Application development

  • All in typescript
  • Provide general features for frameless window
  • Provide a general client-side config service

DOCUMENTATION

Installation

    npm i react-openfin 
    or 
    yarn add react-openfin

React-Openfin & its Materialize UI Implementation

Before continue, make sure you have already generated a sample project via openfin-js-cli or use https://github.com/openfin-js-app/openfin-react-concise as a helper for simplicity.

Big Pic

First of all, thanks to material-ui, a awesome more-than-style lib help us materialize our projects.

If you check the modules I installed in the package.json, you might find out there are two modules which are part of openfin-js-app.

react-openfin-mat-impl, you can tell by the name, it is an implementation of react-openfin via material-ui. And, it is due to this, that react-openfin-mat-impl internally control the version of react-openfin it wanna implement. And app developer does not have to include react-openfin specifically! Besides these, the primary task of react-openfin-mat-impl is to provide reusable components to simplify the openfin app developing. All the details and props of the components exported will be covered in the latter react-openfin-mat-impl section.

react-openfin-scripts is like react-scripts, but it does one more thing, react-openfin-scripts enhances the default setting to adapt the new openfin app requirements, also provide few additional tasks to aid openfin app developing, like serving, packaging, start with openfin app, serve with app and a new dot env profile variable REACT_APP_ENV to load different dotenv config.

Since you do not have to install react-openfin but it exists already brought by react-openfin-mat-impl. In the professional section, we might encounter react-openfin more frequency for advance features. Thus it is not harm to know it for now. react-openfin just provides few react contexts and helper event format to scandalize the way to use Openfin js api. It only focus on the logic instead of views. And react-openfin-mat-impl materialize the views react-openfin trying to expresss.

Conclusion

In a nutshell,

  • if wanna seize a reusable component, check react-openfin-mat-impl.
  • if wanna tweak communicate with the openfin settings, check react-openfin instead.
  • need to write a script to do some scaffolding tasks, check react-scripts, it might already have one already.

Initialization of Openfin JS App

With a big pic of what is what. Time to get hands dirty a little bit.

A regular react-app cannot be an openfin js app, thus we need some general initialization step. Thanks to React & react-openfin, this procedure could be very simple.

src/index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { InitializeReactOpenfin, ReactOpenfin } from 'react-openfin';

import i18n from './i18n';
import hist from './utils/history';

import App from './App';

declare const window:any;

InitializeReactOpenfin({
    fin:window.fin,
    finUuid: process.env.REACT_APP_FIN_UUID,
    i18n,
    hist
});

ReactDOM.render(
    <ReactOpenfin>
        <Suspense fallback={<CircularProgress/>}>
            <I18nextProvider i18n={i18n}>
                <App/>
            </I18nextProvider>
        </Suspense>
    </ReactOpenfin>
    ,
    document.getElementById('root')
);

All app developers need to do is import initializer InitializeReactOpenfin and wrapper ReactOpenfin from react-openfin and fill in the init argument of InitializeReactOpenfin and use ReactOpenfin to wrap all your components. That is it.

However, you might also notice there are two other pals called i18n and history required in the init argument. i18n is instance of the i18next lib to support multi-lang and hist is a js lib history that lets use easily manage session history anywhere. And their setup codes can be find over here i18n.ts and history.ts

Start Openfin JS App

Once react-openfin is initialized, it is the time to start the it. The best place to start it is undoubtedly the root entry App component.

src/App.tsx
import * as React from 'react';
import { useContext, useEffect} from 'react';
import { Router, Route, Switch, Redirect } from 'react-router-dom';
import { ApplicationContext, ConfigContext, MuiTheme } from 'react-openfin';
import { buildMuiTheme } from 'react-openfin-mat-impl';

import CssBaseline from '@material-ui/core/CssBaseline';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import { ThemeProvider } from '@material-ui/styles';

import indexRoutes from './routes';

import hist from './utils/history';

const App:React.FunctionComponent<{}> = (
    {
    }
)=>{

    const {
        state:{
            loading,
        },
        actions:{
            onApplicationStart,
            onChildWinStart,
            onNotificationStart,
        }
    } = useContext(ApplicationContext);

    useEffect(()=>{
        console.log("App Entry::init effect");
        // !!!README!!!
        // make sure to start the application/childWindow/Notification at the Entry component to boot react-openfin
        if (window.name === process.env.REACT_APP_FIN_UUID){
            onApplicationStart();
        }else if (window.location && window.location.pathname.toLowerCase().indexOf('notification')>-1){
            onNotificationStart();
        }else{
            onChildWinStart();
        }
    },[]);

    const {
        config:{
            application:{
                theme
            }
        }
    } = useContext(ConfigContext);


    const muiTheme = buildMuiTheme(theme);

    return (
        <React.Fragment>
            <CssBaseline/>
            <Router history={hist}>
                <MuiThemeProvider theme={muiTheme}>
                    <ThemeProvider theme={muiTheme}>
                        <Switch>
                            {
                                indexRoutes.map((prop:any,key)=>{
                                    if (prop.redirect)
                                        return <Redirect from={prop.path} to={prop.to} key={key}/>;
                                    return <Route path={prop.path} component={prop.component} key={key}/>;

                                })
                            }
                        </Switch>
                    </ThemeProvider>
                </MuiThemeProvider>
            </Router>
        </React.Fragment>
    );
}


export default App;

We import the two context api ApplicationContext ConfigContext from react-openfin. ApplicationContext contains the functions to start the openfin js app.

  • onApplicationStart
  • onNotificationStart
  • onChildWinStart

You can tell by name, one to start main window application, one to start a regular notification and one to start a regular child window.

Since the react app is a SPA, we do not want build each one SPA for each window or app; better share and reuse as much code as possible. Thus for the same App component, we have to make decision and tell them apart from MainWindow, Child Window or Notification.

  • If current window is the only one main window, Openfin will popluate the window.name field with UUID
  • Mentioned in the section, the router path for notification will contain the string of notificaiton.
  • Besides main window and notification, the rest should be the child window.

Thus basing on these condition, we triggered corresponding init function.

As we are going to reuse components from react-openfin-mat-impl, we need to initialize a theme to for material-ui also. react-openfin-mat-impl provides an expected theme builder buildMuiTheme, with the dark/light theme identifier, a muiTheme is created and app developer can still modify the theme before giving to the ThemeProvider.

The rest of children components are loaded routing children via react-router-dom. We will cover the routing comps in the next section.