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-terrific-bridge

v2.0.0

Published

A utility belt for using TerrificJS together with React. Provides module registration and unregistration as well as an action API for event bindings, also supporting Flow and TypeScript.

Downloads

340

Readme

Build Status Coverage Status

TerrificBridge

The Terrific Bridge is used to manage Frontend UI components inside React. It can be used to register components, send and receive actions (bidirectional) and handle application starts & stops. Remember that the TerrificBridge is a singleton and can be instanciated once per Application.

TOC

Package

import TerrificBridgeInstance, { TerrificBridge, TerrificBridgeGlobalAppId, getGlobalApp } from 'react-terrific-bridge';

Dependencies

Peer Dependencies

Contents

TerrificBridgeInstance (default)

Singleton instance which should be used to register any components inside the page.

TerrificBridge

The named export TerrificBridge is the class blueprint of the singleton. You can use it if you need multiple Terrific Applications.

TerrificBridgeGlobalAppId

Will be used to make the application available inside the window object if the debug mode is enabled. You can receive the application via te getGlobalApp() method.

getGlobalApp

Will return the global application instance if the debug mode was enabled before, otherwise it will return undefined;

Installation

It is recommended to use yarn as package manager and to install react-terrific-bridge via yarn, allthough it would work with NPM.

yarn add react-terrific-bridge
npm install --save react-terrific-bridge

Loading the TerrificBridge

The most important thing in the bridge is the load() method, which will bootstrap the Terrific application on the react side. You must call this method at the root entry of your react app to make it possible to register all components. All components which are registered before the load function was called will be automatically initialized during the load() call.

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import TerrificBridge from 'react-terrific-bridge';

class App extends Component {
    componentDidMount() {
        // bootstrap terrific application ...
        TerrificBridge.load();
    }

    render() {
        return <h1>Hello there!</h1>;
    }
}

ReactDOM.render(<App />, document.getElementById('root'));

Registering Component

Just register a Terrific Component and start it inside the componentDidMount hook.

import React, { Component } from 'react';
import TerrificBridge from 'helper/terrific';

export default class Slider extends Component {

     constructor(props) {
        super(props);
        this.componentRef = React.createRef();
    }

    componentDidMount() {
        TerrificBridge.registerComponent(this.componentRef.current);
    }

    componentWillUnmount() {
        TerrificBridge.unregisterComponent(this.componentRef.current);
    }

    render() {
        const { modifier, slides } = this.props;

        return (
            <ul  ref={this.componentRef} className={'m-slider' + (modifier ? 'm-slider--' + modifier : '')} data-t-name="Slider">
                {slides &&
                    slides.map((slide, index) => {
                        return (
                            <li key={index}>
                                <img src={slide.image} alt={slide.alt} />
                                <h5>{slide.caption}</h5>
                                {slide.text && <p>{slide.text}</p>}
                            </li>
                        );
                    })}
            </ul>
        );
    }
}

Registering Component with receive actions

If you register a Component with a Functin factory as second parameter, you can specify receive actions which the Terrific component can execute in the React App. Each Frontend Component has a method called send which is capable to transport N arguments.

import React, { Component } from 'react';
import TerrificBridge from 'helper/terrific';

export default class Slider extends Component {

    constructor(props) {
        super(props);
        this.componentRef = React.createRef();
    }

    componentDidMount() {
        // Trigger via T.Module.Slider.send("shouldUpdate");
        TerrificBridge.registerComponent(this.componentRef.current, {
            shouldUpdate: this.forceUpdate,
        });
    }

    componentWillUnmount() {
        TerrificBridge.unregisterComponent(this.componentRef.current);
    }

    render() {
        const { modifier, slides } = this.props;

        return (
            <ul ref={this.componentRef} className={'m-slider' + (modifier ? 'm-slider--' + modifier : '')} data-t-name="Slider">
                {slides &&
                    slides.map((slide, index) => {
                        return (
                            <li key={index}>
                                <img src={slide.image} alt={slide.alt} />
                                <h5>{slide.caption}</h5>
                                {slide.text && <p>{slide.text}</p>}
                            </li>
                        );
                    })}
            </ul>
        );
    }
}

Send actions to the UI Component

In this part you'll see how to send actions from React to the Frontend UI component. To send actions to the UI component, the Terrific Component needs to provide an object in its root called actions. You can call any action defined in this Object.

import React, { Component } from 'react';
import TerrificBridge from 'helper/terrific';

export default class Slider extends Component {

    constructor(props) {
        super(props);
        this.componentRef = React.createRef();
    }

    componentDidMount() {
        TerrificBridge.registerComponent(this.componentRef.current, {
            update: this.forceUpdate,
        });
    }

    componentWillUnmount() {
        TerrificBridge.unregisterComponent(this.componentRef.current);
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.forceUpdate && nextProps.forceUpdateId) {
            // T.Module.Slider.actions.update(nextProps.forceUpdateId);
            TerrificBridge.action(this.componentRef.current, 'update', nextProps.forceUpdateId);
        }
    }

    render() {
        const { modifier, slides } = this.props;

        return (
            <ul ref={this.componentRef} className={'m-slider' + (modifier ? 'm-slider--' + modifier : '')} data-t-name="Slider">
                {slides &&
                    slides.map((slide, index) => {
                        return (
                            <li key={index}>
                                <img src={slide.image} alt={slide.alt} />
                                <h5>{slide.caption}</h5>
                                {slide.text && <p>{slide.text}</p>}
                            </li>
                        );
                    })}
            </ul>
        );
    }
}

Unregister Component

Just register a Terrific Component and start it inside the componentDidMount hook.

import React, { Component } from 'react';
import TerrificBridge from 'helper/terrific';

export default class Slider extends Component {

    constructor(props) {
        super(props);
        this.componentRef = React.createRef();
    }
    componentDidMount() {
        TerrificBridge.registerComponent(this.componentRef.current);
    }

    componentWillUnmount() {
        TerrificBridge.unregisterComponent(this.componentRef.current);
    }

    render() {
        const { modifier, slides } = this.props;

        return (
            <ul ref={this.componentRef} className={'m-slider' + (modifier ? 'm-slider--' + modifier : '')} data-t-name="Slider">
                {slides &&
                    slides.map((slide, index) => {
                        return (
                            <li key={index}>
                                <img src={slide.image} alt={slide.alt} />
                                <h5>{slide.caption}</h5>
                                {slide.text && <p>{slide.text}</p>}
                            </li>
                        );
                    })}
            </ul>
        );
    }
}

Using the bridge without the window object

The terrific bridge can be used with a custom terrific module, per default it will look for the window.T object in your browser. There are two methods which you can use to override the module:

  1. Override the singleton with a new configuration
import TerrificBridge, { TerrificBridge as TerrificBridgeBlueprint } from '@namics/react-terrific-bridge';
import customTerrificModule from 'terrific';

new TerrificBridgeBlueprint({
    overrideSingletonInstance: true,
    terrific: customTerrificModule,
});

// ...

TerrificBridge.registerComponent(/* ... */);
  1. Override the internal terrific space
import TerrificBridge from '@namics/react-terrific-bridge';
import customTerrificModule from 'terrific';

TerrificBridge.useCustomTerrific(customTerrificModule);

React API

load

Starts the Terrific Application with a configurable set of options.

| #  | Argument Type | Required | | --- | ---------------- | -------- | | 1 | Settings: Object | false |

TerrificBridge.load(settings: Object): void
01 Settings

A configuration set for the Bridge Singleton.

reset

Resets the TerrificBridge state (queue, components, amm.) and must be re-loaded afterwards.

TerrificBridge.reset(): void

registerComponent

| #  | Argument Type | Required | | --- | ------------------------ | -------- | | 1 | DOM Node | true | | 2 | BindingFactory {} | false |

TerrificBridge.registerComponent(node, factory: BindingFactory): void
1 DOM Node

Reference a DOM Node

2 BindingFactory

Can be used for setting actions which the Terrific component can execute.

type BindingFactory = {
    [name: string]: Function,
};
getComponentById

Will return a Terrific Component instance.

TerrificBridge.getComponentById(id: number): Object | void

| #  | Argument Type | Required | | --- | ------------------ | -------- | | 1 | TerrificId: number | true |

01 Terrific ID

The ID of a component (e.g. 198).

unregisterComponent

| #  | Argument Type | Required | | --- | ------------------------ | -------- | | 1 | DOM Node | true |

TerrificBridge.unregisterComponent(node): void
01 DOM Node

Reference for the React Component DOM Node

action

| #  | Argument Type | Required | | --- | ------------------------ | -------- | | 1 | DOM Node | true | | 2 | Action: string | true | | N | Arguments: ...any | false |

TerrificBridge.action(node, action: string, ...args: any): void
1 DOM Node

Reference for the React Component DOM Node

2 Action

The action to trigger in the FE UI component under actions.

N Arguments

Pass N arguments after the React Component and the Action to the FE UI.

Terrific API

Actions

Actions which can be triggered by a react component must be stored directly inside the module under the variable actions, otherwise React won't be able to trigger any action on the UI side.

actions = { [actionName]: () => {} }: Object

Example

($ => {
    'use strict';

    T.Module.Slider = T.createModule({
        actions: {},
        start(resolve) {
            this._setActions();
            resolve();
        },
        stop() {
            this._events.off().disconnect();
        },
        _setActions() {
            this.actions = {
                update() {
                    // Place some logic here
                },
            };
        },
        /* ... more methods */
    });
})(jQuery);

Send

send(actionName: string [, ...args]): void

The send method will be created by the react side of the application. This method allows the UI to trigger custom functions inside a react component e.g. a actionCreator, class methods or something similar.

Example

($ => {
    'use strict';

    T.Module.Calculator = T.createModule({
        start(resolve) {
            $(this._ctx)
                .find('js-a-button--submit')
                .on('click', ev => {
                    this.onSubmit.call();
                });

            resolve();
        },
        onSubmit() {
            this.send('submit', this.someValueHere, ['more ...']);
        },
        /* ... more methods */
    });
})(jQuery);