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

@formsort/web-embed-api

v2.7.0

Published

Embed Formsort flows within other webpages

Downloads

16,178

Readme

@formsort/web-embed-api

Embed Formsort flows within other webpages, with communication between the embed and the parent page.

If you're using React, see @formsort/react-embed which is a handy wrapper around this.

Usage

First, install

yarn add @formsort/web-embed-api
npm install @formsort/web-embed-api --save

Then, initialize the embed and load a flow.

Documentation

FormsortWebEmbed(rootEl: HTMLElement, config?: IFormsortWebEmbedConfig)

Initializes a Formsort iframe as a child of the rootEl provided.

const embed = FormsortWebEmbed(document.body);

The optional config object has the following interface:

interface IFormsortWebEmbedConfig {
  useHistoryAPI?: boolean; // Default: false
  autoHeight?: boolean; // Default: false
  style?: {
    width?: CSSStyleDeclaration['width'];
    height?: CSSStyleDeclaration['height'];
  };
  authentication?: {
    idToken?: string; // ID Token for authenticated flows
  };
  origin?: string; // optional -- use if you want to load your flow in a custom damain. e.g. https://acme-flow.com
}

config properties

  • useHistoryAPI: When redirecting, should we use the HTML5 History API (namely, window.pushState), or just change the URL in its entirety?

    Helpful if you have a single-page app and want to change the container's URL without reloading the entire page. Note that you'll have to listen to the popstate event on the embedding window to detect this navigation.

  • autoHeight: Should the embedding <iframe> resize so that it matches the content of the currently loaded flow?

  • style: CSS properties to be applied to the iframe container.

  • authentication.idToken: When the Flow requires an ID token, this can be used to provide it.

loadFlow(clientLabel: string, flowLabel: string, variantLabel?: string, queryParams?: Array<[string, string]>) => void

Starts loading a Formsort variant, or a flow.

Note that variantLabel is optional. If it is not provided, a variant will be chosen from the flow:

  • Based on weights, if weights are assigned

  • At random, if no weights are assigned

unloadFlow() => void

Remove inserted iFrame and all its event listeners.

setSize(width: number, height: number) => void

Set the CSS size of the embed.

You may also style the embed's iframe using CSS - it is the iframe child of the rootEl, so you'd use the selector #rootEl > iframe.

Event listeners

embed.addEventListener(eventName: key of IEventMap, fn: IEventMap[eventName]) => void

If the flow is embedded in a whitelisted domain, each event listener will be passed the user's answers.

The events include:

FlowLoaded (answers?: { [key: string]: any } ) => void

Set a callback function to be called when the Formsort flow has loaded completely.

Note that this is more accurate than listening for the iframe's load event, as this is sent from within the Formsort application code and not merely when the DOM is loaded.

You can listen for this to do things like hide the frame container, or display a loading indicator, until everything is loaded to ensure a seamless initial experience.

const embed = FormsortWebEmbed(document.body);
embed.addEventListener('FlowLoaded', () => {
  console.log('Flow has loaded!');
});
embed.loadFlow('formsort', 'onboarding', 'main');

FlowFinalized (answers?: { [key: string]: any }) => void

Set a callback to be called when the flow is complete, meaning the user has finished all of the steps available to them.

Useful for performing an action after the flow is complete, such as displaying a congratulations or starting a payment process.

FlowClosed (answers?: { [key: string]: any }) => void

Set a callback to be called when the user abandons the flow before finalizing it.

Note that this is only possible if your style set defines a close button.

StepLoaded (answers?: { [key: string]: any }) => void

Set a callback to be called when a new step is loaded.

This will happen once after the flow is loaded, if the user hasn't previously completed the flow. Subsequently, this event will happen following the completion of each step, except for the completion of the final step.

StepCompleted (answers?: { [key: string]: any }) => void

Set a callback to be called when a step is completed. This includes the completion of the final step, before the "FlowFinalized" event.

ResponderStateUpdated (answers?: { [key: string]: any }) => void

Set a callback to be called whenever the responder state is updated, corresponding to when Formsort itself saves responder answers and delivers them to integrations.

By default, this will correlate roughly to the StepCompleted event, since the default behavior is to save answers only when advancing between steps. However, if you have enabled autoSave, then you will receive answers a short delay after a user has finished entering data.

redirect ({ url: string, answers?: { [key: string]: any } }) => ({ cancel?: boolean }) | undefined

Set a callback to customize the way Formsort handles redirects. To cancel Formsort's handling of the redirect, return:

{
  cancel: true;
}

If the callback does not return a value, Formsort will handle the redirect as usual.

This is helpful if you're embedding Formsort within a single-page app that has custom URL route handling.

unauthorized () => void

Set a callback for unauthorized Formsort messages.

This callback is used when the Formsort Flow is authenticated but the ID token is missing or invalid.

Development

By default, the web embed accesses the production formsort servers. If you would like to point to another flow server, set origin in the config to the correct base URL, for example:

FormsortWebEmbed(document.body, { origin: 'http://localhost:4040' });