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-callable

v1.2.7

Published

Callable components, that can be called from anywhere in your application.

Downloads

59

Readme

react-callable

Callable components, that can be called from anywhere in your application.

WARNING: Further development is slow downed

The repository is not under active development. The reason for that decision is that I slightly changed my opinion on the matter of callable components. Although, I still think of it as an interesting idea of being able to do such stuff. However, I don't have much time to properly pay attention to this repo.

Despite that sad fact, all contributions are still welcomed and appreciated because they allow this project to move further and evolve.

Build Status

Dependency Status devDependencies Status

Inline docs

https://nodei.co/npm/react-callable.png?downloads=true&downloadRank=true&stars=true

Table of Contents

  1. Why?
  2. Concept
  3. Usage
  4. Examples

Why?

There are already a few libs that allows you to do the same things, however...this lib proposes another approach to the matter.

Main purpose of that library is to allow the creation of any type of callable components. In ideal, that means, that you're able to create your own callable modal dialogs, messages on different screensides, tooltips, popovers and so on.

It does not mean, that react-callable will do all the stuff. It just gives some sort of help.

Concept

When imagine what can be done, I made up 5 concepts to be reached.

  1. Callable should somehow be called
  2. Callable should somehow be closed
  3. Callable should somehow be accessed
  4. Callable should somehow be updated
  5. Callable should somehow be chained

Usage:

Installation

npm i --save react-callable

or

yarn add react-callable

API

createCallable
import { createCallable } from 'react-callable';

Function that accepts an object of settings with following structure:

1. async

Type: boolean
Defines, whether callable will return promise

2. arguments

Type: Array<string>
If defined, then on callable call you should pass arguments in the same order as defined in your arguments property.

3. callableId

Type: number | string
Is used in callable container creation.

4. customRoot

Type: element | function | string
By default callable is rendered in outside-root, that is created when first createCallable is called. But sometimes, we would want to render it in some other place. To make this we can use customRoot.
It accepts three type of data:

  1. string
  2. node reference
  3. function

String will be used as a query selector to find dom node.
Function will be resolved on callable call and should return dom node.

5. dynamicRoot

Type: boolean
It allows to pass reference to a target element as the very last argument into a call (at least it should be second arg, take it into account, examples below)

6. directInjection

Type: boolean
If passed, then callable will be rendered without any wrappers inside specified customRoot

Also you can pass no params at all into createCallable. (see Plain callable)

Examples

1 - Async Callable:

Confirm/index.js
import { createCallable } from "react-callable";

const confirmCreator = createCallable({ async: true, arguments: ['title', 'description', 'submitStatus', 'cancelStatus'], callableId: 1 });

const Confirm = ({ title, description, submitStatus, cancelStatus, conclude }) => {
  return (
    <div className={styles.confirmWrapper}>
      <div className={styles.confirmBackdrop} onClick={() => { conclude(cancelStatus); }} />
      <div className={styles.confirm}>
        <div className="header">
          <h1>
            {title}
          </h1>
        </div>
        <div className="content">
          <p>
            {description}
          </p>
        </div>
        <div className="actions">
          <div className="btn-group" style={{ float: 'right' }}>
            <button className="btn" onClick={() => conclude(submitStatus)}>Submit</button>
            <button className="btn" onClick={() => conclude(cancelStatus)}>Cancel</button>
          </div>
        </div>
      </div>
    </div>
  );
};

const confirm = confirmCreator(Confirm);

export default confirm;
App.js
import React, {Component} from 'react';
import confirm from './components/Confirm';
import classNames from './styles.module.scss';

class App extends Component {

  openConfirm = () => {
    confirm('Hello World', 'Called by React Callable', 'submitted', 'cancelled').then((response) => alert(`confirm is ${response}`));
  };

  render() {
    return (
      <div className={classNames.app}>
        <button className="btn large" onClick={this.openConfirm}>
          Confirm me
        </button>
      </div>
    );
  }
}

export default App;

2 - Callbacks Callable:

Confirm/index.js
import { createCallable } from "react-callable";

const confirmCreator = createCallable({ async: true, arguments: ['title', 'description', 'onSubmit', 'onCancel'], callableId: 1 });

const Confirm = ({ title, description, onSubmit, onCancel, conclude }) => {
  return (
    <div className={styles.confirmWrapper}>
      <div className={styles.confirmBackdrop} onClick={() => { onCancel(); conclude(); }} />
      <div className={styles.confirm}>
        <div className="header">
          <h1>
            {title}
          </h1>
        </div>
        <div className="content">
          <p>
            {description}
          </p>
        </div>
        <div className="actions">
          <div className="btn-group" style={{ float: 'right' }}>
            <button className="btn" onClick={() => {onSubmit(); conclude();}}>Submit</button>
            <button className="btn" onClick={() => {onCancel(); conclude();}}>Cancel</button>
          </div>
        </div>
      </div>
    </div>
  );
};

const confirm = confirmCreator(Confirm);

export default confirm;
App.js
import React, {Component} from 'react';
import confirm from './components/Confirm';
import classNames from './styles.module.scss';

class App extends Component {

  openConfirm = () => {
    confirm('Hello World', 'Called by React Callable', () => console.log('Harry'), () => console.log('Volan'));
  };

  render() {
    return (
      <div className={classNames.app}>
        <button className="btn large" onClick={this.openConfirm}>
          Confirm me
        </button>
      </div>
    );
  }
}

export default App;

Now I will try to show only main differences. Sorry, but code takes a lot of space.

3 - Dynamic Root

import { createCallable } from "react-callable";

// create confirm with 4 arguments and enable passing root as very last argument
const confirmCreator = createCallable({ 
    arguments: [
        'title', 
        'description', 
        'onSubmit', 
        'onCancel'
    ], 
    dynamicRoot: true 
});

// assume that Confirm was already defined somewhere
const confirm = confirmCreator(Confirm);

// because we turned on dynamicRoot, 
// confirm will look for the very last argument, 
// i.e. argument, that is be placed after all arguments, 
// described in createCallable function
confirm(
    'Are you sure?', 
    "You're going to do something?", 
    () => alert('Just do it!'), 
    () => alert("It's time to stop!"), 
    document.querySelector('body > .custom-container')
)

4 - Plain callable

import { createCallable } from "react-callable";

// create confirm with 4 arguments and enable passing root as very last argument
const confirmCreator = createCallable();

// assume that Confirm was already defined somewhere
const confirm = confirmCreator(Confirm);

// as soon as we didn't declare arguments createCallable
// callable will assume, that the very first argument of confirm call is your props 
// and will be pass as is to the component + callable special `conclude` prop for concluding itself
confirm({
    title: 'Are you sure?', 
    description: "You're going to do something?", 
    onSubmit: () => alert('Just do it!'), 
    onCancel: () => alert("It's time to stop!"), 
})

TODO:

  • [x] Optional callableId
  • [x] Optimize roots logic
  • [x] Allow to pass root in a call function
  • [X] Direct node injection
  • [X] React Portals Support
  • [ ] Access root and container inside callable
  • [ ] More examples: semantic
  • [ ] Callable update
  • [ ] Callable chain
  • [ ] onBeforeRender and onAfterConclude callbacks

P.S.

To be honest, I made this package for self-usage, but I will be proud, if someone else will find it useful.