Repository: mwood23/preact-island Branch: master Commit: 026c2d0c2d7a Files: 49 Total size: 78.9 KB Directory structure: gitextract_f_30itg8/ ├── .editorconfig ├── .gitignore ├── .npmignore ├── .prettierrc ├── CHANGELOG.md ├── LICENSE ├── README.md ├── config/ │ └── setupTests.js ├── example/ │ ├── declaration.d.ts │ ├── package.json │ ├── src/ │ │ ├── call-to-action.island.css │ │ ├── call-to-action.island.tsx │ │ ├── email-subscribe.island.css │ │ ├── email-subscribe.island.tsx │ │ ├── global.d.ts │ │ ├── index.ts │ │ ├── pokemon.clean.island.tsx │ │ ├── pokemon.component.tsx │ │ ├── pokemon.global.island.tsx │ │ ├── pokemon.initial-props.island.tsx │ │ ├── pokemon.inline.island.tsx │ │ ├── pokemon.island.css │ │ ├── pokemon.island.tsx │ │ ├── pokemon.props-selector.island.tsx │ │ ├── pokemon.replace.island.tsx │ │ ├── template.html │ │ └── utils.ts │ ├── tsconfig.json │ └── webpack.config.js ├── example-react/ │ ├── declaration.d.ts │ ├── package.json │ ├── src/ │ │ ├── call-to-action.island.css │ │ ├── call-to-action.react.island.tsx │ │ ├── pokemon.component.tsx │ │ ├── pokemon.island.css │ │ ├── pokemon.react.island.tsx │ │ └── utils.ts │ └── tsconfig.json ├── jest.config.js ├── package.json ├── src/ │ ├── index.ts │ ├── island-web-components.tsx │ ├── island.ts │ ├── lib.ts │ └── tests/ │ ├── helpers/ │ │ ├── getById.ts │ │ └── inlineScript.tsx │ ├── island.props.test.tsx │ └── island.selector.test.tsx └── tsconfig.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ root = true [*] indent_style = space indent_size = 2 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true end_of_line = lf ================================================ FILE: .gitignore ================================================ *.jks *.p8 *.p12 *.mobileprovision *.orig.* web-build/ node_modules dist **/public/build .netlify .cache .next .env stats.html *tar.gz # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies /.pnp .pnp.js # testing coverage # misc .DS_Store npm-debug.log* yarn-debug.log* yarn-error.log* lerna-debug.log ui-debug.log # Dependencies jspm_packages # Serverless .serverless # Dynamodb .dynamodb # Webpack .webpack test-results test-report tests-out playwright-report # Local folder local # Misc .DS_Store *.pem Thumbs.db size-plugin.json ================================================ FILE: .npmignore ================================================ !dist example/ coverage/ docs/ jest.config.js tsconfig.json config/ .editorconfig .prettierrc ================================================ FILE: .prettierrc ================================================ { "singleQuote": true, "trailingComma": "all", "semi": false, "endOfLine": "auto" } ================================================ FILE: CHANGELOG.md ================================================ ## 1.0.4 - Add subtree checking for prop scripts so that it works well in a React rerender context ## 1.0.5 - Bad publish, oopsie! Use 1.0.6. ## 1.0.6 - Fix peer deps so it correctly resolves. Thanks [@rschristian](https://github.com/rschristian) ## 1.1.0 - 🧩 Experimental web component support including web component portals! This API may change drastically over time so if you use it keep that in mind. If you have ideas on how to improve it file an issue! - Migrate examples over to webpack and off of Microbundle + Preact CLI. Now the development server injects the scripts just like you would in production to give you the closest environment to. - Internal island lib now ships with support for the shadow dom (even if you don't want to use the build in island web components API) - Documentation updates ## 1.1.1 - Fixed bug where rendering a web component multiple times on a page would not function as expected. This was due to the custom element being defined twice causing everything to silently fail. ## 1.1.2 - Fixed bug where data-mount-in did not function with web components ## 1.2.0 - Fix package.json exports field ================================================ FILE: LICENSE ================================================ Copyright 2022 Marcus Wood Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================

🏝 Preact Island

A 1.3kB module that helps you ship your own slice of paradise to any website. Especially useful for Shopify apps or CMS websites.

[![downloads][downloads-badge]][npmcharts] [![version][version-badge]][package] [![Supports Preact and React][preact-badge]][preact] [![MIT License][license-badge]][license]
> 🚀 Looking for a zero config starter to build islands fast? Check out [preact-island-starter](https://github.com/mwood23/preact-island-starter) Sometimes you need to embed a component onto someone else's website. This could be a Shopify widget, email sign up form, CMS comment list, social media share icon, etc. Creating these experiences are tedious and difficult because you aren't in control of the website your code will be executed on. Preact Island helps you build these experiences by adding a lightweight layer on top of Preact. For <5kB, you get a React style workflow (with hooks!), and a framework for rendering your widget with reactive props. ## Features - 🚀 Render by selector, inline, or by a specific attribute given on the executed script - ⚛️ Based on Preact, no special compiler or anything needed to render an island - 🙏 5 ways to pass in props to your component - 🧩 Experimental support for web components (including web component portals) - 🪄 All components are reactive to prop changes causing rerenders (not remounts) - 👯‍♀️ Create as many instances of your component as you need with a single island - 🧼 Does not mutate the `window`. Use as many islands as you'd like on one page! - 🐣 Less than 1.3kB - ☠️ Supports replacing the target selector - 🏔 React friendly with `preact/compat` - 🔧 Manually trigger rerenders with props - 🐙 Fully tested with Preact testing library - 👔 Fully typed with TypeScript ## Examples - [Basic element placement with props](https://codesandbox.io/s/preact-island-element-placement-with-props-8kzmj5) - [Reactive props](https://codesandbox.io/s/reactive-prop-updates-uqvof7) - [Multiple islands](https://codesandbox.io/s/multiple-islands-8xvjqw) - [Props script](https://codesandbox.io/s/props-selector-70ks1g) - [Replace selector](https://codesandbox.io/s/replace-selector-z2rogw) - [Multiple host elements](https://codesandbox.io/s/multiple-host-elements-q6ot5q) - [Clean host element](https://codesandbox.io/s/clean-host-element-i35nlt) - [Global island](https://codesandbox.io/s/global-island-zqco9p) - [Inline script](https://codesandbox.io/s/inline-script-1qm5q8) - [Executed script props](https://codesandbox.io/s/preact-island-element-placement-current-script-props-0dwlyo) - [Interior script props](https://codesandbox.io/s/interior-script-props-z5rcxg) - [Mount In script attribute](https://codesandbox.io/s/mount-in-property-z5rcxg) - [React with Props](https://codesandbox.io/s/preact-island-element-placement-with-props-react-49mjrg) - [React Portals with Preact Island](https://codesandbox.io/s/preact-island-element-placement-with-props-react-tdss2p) ## Installation ```sh npm install --save preact-island ``` ## Usage ```tsx import { createIsland } from 'preact-island' const Widget = () => { return
awesome widget!
} const island = createIsland(Widget) island.render({ selector: '[data-island="widget"]', }) ``` ## API ### createIsland Creates a new island instance with a passed in component. Returns a bag of props/methods to work with your island. ```tsx import { createIsland } from 'preact-island' const Widget = () => { return
awesome widget!
} const island = createIsland(Widget) ``` ### createIsland().render Renders an island to the DOM given options. ```ts const island = createIsland(Widget) island.render({...}) ``` #### options ````ts /** * A query selector target to create the widget. This is ignored if inline is passed or if a `data-mount-in` attribute * is appended onto the executed script. * * @example '[data-island="widget"]' */ selector?: string /** * If true, removes all children of the element before rendering the component. * * @default false * * @example * ```html *
*
some other content
*
some other content
*
some other content
*
* ``` * * // turns into * * ```html *
*
your-widget
*
* ``` * * // turns into * * ```html *
your-widget
* ``` */ replace?: boolean /** * Renders the widget at the current position of the script in the HTML document. * * @default false * * @example * ```html *
*
some content here
* *
some content here
*
* ``` * * // turns into * * ```html *
*
some content here
* *
your widget
*
some content here
*
* ``` */ inline?: boolean /** * Initial props to pass to the component. These props do not cause updates to the island if changed. Use `createIsland().rerender` instead. */ initialProps?: P /** * A valid selector to a script tag located in the HTML document with a type of either `text/props` or `application/json` * containing props to pass into the component. If there are multiple scripts found with the selector, all props are merged with * the last script found taking priority. */ propsSelector?: string ```` ### createIsland().rerender Triggers a rerenders of the island with the new props given. ```ts const island = createIsland(Widget) island.render({ selector: '[data-island="widget"]' }) island.rerender({ new: 'props' }) ``` ### createIsland().destroy Destroys all instances of the island on the page and disconnects any associated observers. ```ts const island = createIsland(Widget) island.render({ selector: '[data-island="widget"]' }) island.destroy() ``` ## Selecting Mount Point from Script You can override the `selector` given to render by passing `data-mount-in` to the script. [Example](https://codesandbox.io/s/mount-in-property-z5rcxg) ```html

Special mount

``` ## Passing Props Props can be passed to your widget various ways. You can choose to pass props multiple different ways to your island where they'll be merged in a defined order. ### Merging Order Props are merged in the following order (from lowest to highest specificity): 1. Initial props 2. Element props 3. Executed script props 4. Props selector props 5. Interior script props ### Data Props All props located on an HTML element use `data-`. You can name them any of the following ways: - `data-background-color` => `backgroundColor` - `data-prop-background-color` => `backgroundColor` - `data-props-background-color` => `backgroundColor` Under the hood all of these element will be transformed to `camelCase` and passed to your component. ### Initial Props Default props can be passed on render to an island. You can render many islands and give them all different initial props. Initial props do not cause rerenders if updated. Use `createIsland.rerender` instead. ```ts import { createIsland } from 'preact-island' const Widget = () => { return
awesome widget!
} const island = createIsland(Widget) island.render({ selector: '[data-island="widget"]', initialProps: { color: '#000000', }, }) island.render({ selector: '[data-island="widget"]', initialProps: { color: '#ffffff', }, }) // Will render two instances of the island with different color props ``` ### Element Props Props can be placed on host elements and passed to your component. These props are reactive and will cause rerenders on changes. [Example](https://codesandbox.io/s/preact-island-element-placement-with-props-8kzmj5) ```html
``` ### Executed Script Props Props can be placed on the script tag that's evaluated to the create your island. These props are reactive and will cause rerenders on changes. [Example](https://codesandbox.io/s/preact-island-element-placement-current-script-props-0dwlyo) ```html
``` ### Props Selector Props Props can be placed inside of a ` ``` ### Interior Script Props Props can be placed inside of a ` ``` ## React Compatibility - [React with Props](https://codesandbox.io/s/preact-island-element-placement-with-props-react-49mjrg) - [React Portals with Preact Island](https://codesandbox.io/s/preact-island-element-placement-with-props-react-tdss2p) Preact Island fully supports React using [preact/compat](https://preactjs.com/guide/v10/switching-to-preact). This allows you to bring your existing React components over to Preact to get great performance gains without needing to rewrite your components. Check out the `example-react` folder to a demo repo that reproduces some of the Preact islands as React islands. Depending on what you import from React, using Preact + Preact Island can result in a **15x smaller bundle** for the same functionality and no code changes needed on your end. ### Bundle Sizes #### React ![react only](./docs/bundle-react-only.png) #### React + Preact/compat ![react with preact/compat](./docs/bundle-react-with-compat.png) #### Preact ![preact](./docs/bundle-preact.png) ## Adding Styles You can add styles to your island just like any other component. If you're island will be running on someone else's website be mindful of the global CSS scope! Prefix all of your classes with a name `island__` or use CSS modules to make sure those styles don't leak. Do not use element selectors like `p` or `h2`. ### Including Styles Preact Island takes no opinions on how CSS is included for your islands. There are two main options: **Inline the CSS into the bundle** This is what the `/example` islands do. ```tsx import { createIsland } from 'preact-island' import style from './email-subscribe.island.css' document.head.insertAdjacentHTML('beforeend', ``) const Widget = () => { return (

Join our newsletter

{/* ... */}
) } const island = createIsland(Widget) island.render({ selector: '[data-island="email-subscribe"]', }) ``` ```html ``` **Pros:** - The consumer of the script doesn't need to include an external stylesheet - There's only one request for rendering the entire widget **Cons:** - Bloats the bundle - The CSS file itself won't be able to be cached **Use an external stylesheet** ```html ``` **Pros:** - The CSS can be cached in the browser - Doesn't bloat the JS bundle **Cons:** - Unless your script creates an element to automatically request the stylesheet the consumer of your script will need to add two things not one ### CSS Libraries It's not recommending to use CSS libraries when developing islands since they're meant to be small and ran everywhere. Some libraries come with opinionated CSS resets and other global CSS styles that could break the consuming website of your island. They are also going to be large. If you need a CSS library, use something that has a just in time compilation step like [Tailwind](https://tailwindcss.com/docs/installation) to minimize the excess CSS. Or you can use something like [Vanilla-Extract](https://vanilla-extract.style/) to build your own zero runtime cost Tailwind. ## Building Your Islands Any modern bundler will work with Preact Island. If you are looking for a script that will run on a webpage you need the `UMD` format. The `/example` project has a demo setup using `webpack`. It works extremely well if you have multiple islands because it can produce multi-entry point bundles. ### Naming Conventions Make sure to name your bundles `kebab-case` since they'll be served over HTTP. Case sensitive URLs can be fiddly depending on browser! ## Hosting Your Islands You can host your files on anywhere you would typically host websites. Vercel, Cloudflare Workers, and Netlify all work great. Netlify recently [changed their prices](https://answers.netlify.com/t/upcoming-changes-to-netlify-plans/52482/158) causing it to be prohibitively expensive depending on your team size. > Make sure to use your own domain when hosting these assets. If you use their subdomain like `something.mynetlify.com` and embed those onto third party websites you may get locked in. ## The Callsite for Your Island When you are consuming the bundled snippet it's important not to block rendering on a consuming page. When a browser loads a webpage and sees a `script` tag it executes that script immediately blocking render. Islands should be independent of the consuming page so it is safe to use the `async` property. See [async vs defer](https://javascript.info/script-async-defer) for more information. > The only exception to this is if you are putting the island on the window and want to run a script after it. Check out [global island](https://codesandbox.io/s/global-island-zqco9p) for an example. Do this: ```html ``` Not this: ```html ``` ## Differences to Preact Habitat This library was heavily inspired by [Preact Habitat](https://github.com/zouhir/preact-habitat). Key differences: - Components rerender based on prop changes. This can be a `data-prop` attribute change on a host element, inside of a props script tag, or event on the executed script. - You can add props to script element itself and they're passed to the component - You can add props to a script tag that lives anywhere in the document and sync it up to the component - You can replace the target container instead of rendering inside of it if you choose - No double loading when mounting the component - Calling render multiple times create many components - There is a `rerender` method that allows you to manually alter props for the component - All dataset attributes (`data-` on an element) are passed as props - There is no `clientSpecified` flag. If you declare a `data-mount-in` prop on a script it will take priority over the `selector` given at `render`. ## Alternatives - [StencilJS](https://stenciljs.com/): A web components toolchain that feels sort of like React and Angular put together. The JSX core is lifted from Preact's internals. It has a lot of nice features like automatic documentation and other nice to haves since it has its own compiler. It feels tailored towards design system and doesn't have the flexibility of prop injection that Preact Island does. It's also another tool to adopt with it's own patterns. All you have to do with Preact Island is bring your component. - [Lit](https://lit.dev/docs/): A web components framework by Google. Does not require a compilation step and weights in around 5Kb (roughly the same as Preact Island + Preact). Doesn't use a virtual dom for diffing and feels like a nice layer on top of web components. Does not support JSX or a React style workflow. ## Credits A huge thank you to [zouhir](https://github.com/zouhir) who is the author of [preact-habitat](https://github.com/zouhir/preact-habitat). This library is heavily inspired by his work on that library. Artwork by [vik4graphic](https://lottiefiles.com/vik4graphic) ## License [MIT](LICENSE) - Copyright (c) [Marcus Wood](https://www.marcuswood.io/) [version-badge]: https://img.shields.io/npm/v/preact-island.svg?style=flat-square [package]: https://www.npmjs.com/package/preact-island [downloads-badge]: https://img.shields.io/npm/dm/preact-island.svg?style=flat-square [npmcharts]: http://npmcharts.com/compare/preact-island [license-badge]: https://img.shields.io/npm/l/preact-island.svg?style=flat-square [license]: https://github.com/mwood23/preact-island/blob/master/LICENSE [preact-badge]: https://img.shields.io/badge/%E2%9A%9B%EF%B8%8F-preact-6F2FBF.svg?style=flat-square [preact]: https://preactjs.com [module-formats-badge]: https://img.shields.io/badge/module%20formats-umd%2C%20cjs%2C%20es-green.svg?style=flat-square [github-star]: https://github.com/mwood23/preact-island/stargazers ================================================ FILE: config/setupTests.js ================================================ import 'regenerator-runtime/runtime' import '@testing-library/jest-dom' import preact from 'preact' import * as hook from 'preact/hooks' global.preact = preact global._hooks = hook ================================================ FILE: example/declaration.d.ts ================================================ declare module '*.css' ================================================ FILE: example/package.json ================================================ { "name": "preact-island-examples", "version": "0.1.0", "description": "", "license": "MIT", "main": "dist/index.js", "umd:main": "dist/index.umd.js", "module": "dist/index.module.js", "source": "src/index.tsx", "files": [ "dist" ], "scripts": { "build": "webpack --env prod", "dev": "webpack serve --env dev" }, "dependencies": { "clsx": "^1.2.1" }, "devDependencies": { "@babel/preset-env": "^7.18.9", "@babel/preset-react": "^7.18.6", "@babel/preset-typescript": "^7.18.6", "@types/webpack": "^5.28.0", "babel-loader": "^8.2.5", "css-loader": "^6.7.1", "html-webpack-plugin": "^5.5.0", "terser-webpack-plugin": "^5.3.5", "webpack": "^5.74.0", "webpack-cli": "^4.10.0", "webpack-dev-server": "^4.9.3" } } ================================================ FILE: example/src/call-to-action.island.css ================================================ .cta_button { background-color: none; border: none; background-color: #294eab; border-radius: 5px; padding: 10px; font-weight: bold; color: white; cursor: pointer; font-family: inherit; } .cta__modal-dimmer { position: fixed; display: none; z-index: 90; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.6); } .cta__modal-dimmer--visible { display: block; animation: show 0.2s; animation-fill-mode: forwards; } .cta__modal { position: fixed; outline: none; z-index: 100; background-color: white; display: none !important; width: 380px; border-radius: 24px; padding: 34px 21px; left: 50%; top: 50%; transform: translate(-50%, -50%); font-family: inherit; overflow-y: auto; height: 650px; text-align: center; } .cta__modal--visible { display: block !important; animation: show 0.3s; animation-fill-mode: forwards; } .cta__modal img { width: 100%; margin-bottom: 1rem; } ================================================ FILE: example/src/call-to-action.island.tsx ================================================ import { createIslandWebComponent, WebComponentPortal } from '../../src' import { useState } from 'preact/hooks' import style from './call-to-action.island.css' import cx from 'clsx' const Widget = ({ backgroundColor }: { backgroundColor?: string }) => { const [isOpen, setIsOpen] = useState(false) return (
{isOpen && (

Portals work with web component islands too!

)} {isOpen && (
setIsOpen(false)} /> )}
) } const name = 'call-to-action' const island = createIslandWebComponent(name, Widget) island.render({ selector: name, }) island.injectStyles(style) ================================================ FILE: example/src/email-subscribe.island.css ================================================ .email__container { border-radius: 5px; border: 1px solid #eae7e7; padding: 1rem; } .email__title { font-family: inherit; display: block; margin-bottom: 1rem; font-weight: bold; color: #333333; font-size: 1.3rem; } .email__input { font-family: inherit; display: block; margin-bottom: 1rem; } .email__input input { display: block; font-family: inherit; background-color: #efefef; padding: 5px 10px; border: none; border-radius: 5px; } .email__submit { background-color: none; border: none; background-color: #2f3a54; border-radius: 5px; padding: 10px; font-weight: bold; color: white; cursor: pointer; font-family: inherit; } ================================================ FILE: example/src/email-subscribe.island.tsx ================================================ import { createIsland } from '../../src' import { useState } from 'preact/hooks' import { injectCSS } from './utils' import style from './email-subscribe.island.css' injectCSS(style) const Widget = ({ showEmail = true, showName = true, ...rest }: { showEmail: boolean showName: boolean }) => { const [value, setValue] = useState({ name: '', email: '' }) return (

Join our newsletter

{ alert(`Submitted with: ${value.name}, ${value.email}`) }} > {showName && ( )} {showEmail && ( )}
) } const island = createIsland(Widget) island.render({ selector: '[data-island="email-subscribe"]', }) ================================================ FILE: example/src/global.d.ts ================================================ declare module '*css' ================================================ FILE: example/src/index.ts ================================================ // This file is only used for developing the snippet in the sandbox import './call-to-action.island' import './email-subscribe.island' import './pokemon.island' ================================================ FILE: example/src/pokemon.clean.island.tsx ================================================ import { createIsland } from '../../dist/index.module' import { Pokemon } from './pokemon.component' import style from './pokemon.island.css' import { injectCSS } from './utils' injectCSS(style) const island = createIsland(Pokemon) island.render({ selector: '[data-island="pokemon"]', clean: true, }) ================================================ FILE: example/src/pokemon.component.tsx ================================================ import { useEffect, useState } from 'preact/hooks' export const Pokemon = ({ pokemon }: { pokemon: string }) => { const [pokemonData, setPokemonData] = useState(null) const [loading, setLoading] = useState(false) const [error, setError] = useState('') useEffect(() => { if (!pokemon) return setLoading(true) fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon}`) .then((d) => d.json()) .then((d) => { setLoading(false) setPokemonData(d) setError('') }) .catch((e) => setError(e.message || 'Something went wrong!')) }, [pokemon]) if (error) { return
{error}
} if (loading) { return
Loading...
} if (!pokemonData) { return (
Select a pokemon to see info
) } return (
{pokemonData.name}

Name: {pokemonData.name}

Number: {pokemonData.id}

) } ================================================ FILE: example/src/pokemon.global.island.tsx ================================================ import { createIsland } from '../../dist/index.module' import { Pokemon } from './pokemon.component' import style from './pokemon.island.css' import { injectCSS } from './utils' injectCSS(style) const island = createIsland(Pokemon) // @ts-expect-error - We're mutating the window to add this window._pokemon = island ================================================ FILE: example/src/pokemon.initial-props.island.tsx ================================================ import { createIsland } from '../../src' import { Pokemon } from './pokemon.component' import style from './pokemon.island.css' import { injectCSS } from './utils' injectCSS(style) const island = createIsland(Pokemon) island.render({ selector: '[data-island="pokemon"]', initialProps: { pokemon: 3, }, }) ================================================ FILE: example/src/pokemon.inline.island.tsx ================================================ import { createIsland } from '../../src' import { Pokemon } from './pokemon.component' import style from './pokemon.island.css' import { injectCSS } from './utils' injectCSS(style) const island = createIsland(Pokemon) island.render({ inline: true, }) ================================================ FILE: example/src/pokemon.island.css ================================================ .pokemon__container { border-radius: 5px; border: 1px solid #eae7e7; padding: 1rem; display: inline-block; } .pokemon__image { width: 200px; height: 200px; text-align: center; } .pokemon__info { color: #2b2e34; margin-bottom: 5px; } ================================================ FILE: example/src/pokemon.island.tsx ================================================ import { createIsland } from '../../src' import { Pokemon } from './pokemon.component' import style from './pokemon.island.css' import { injectCSS } from './utils' injectCSS(style) const island = createIsland(Pokemon) island.render({ selector: '[data-island="pokemon"]', }) ================================================ FILE: example/src/pokemon.props-selector.island.tsx ================================================ import { createIsland } from '../../src' import { Pokemon } from './pokemon.component' import style from './pokemon.island.css' import { injectCSS } from './utils' injectCSS(style) const island = createIsland(Pokemon) island.render({ selector: '[data-island="pokemon"]', propsSelector: '[data-island-props="test-island"]', }) ================================================ FILE: example/src/pokemon.replace.island.tsx ================================================ import { createIsland } from '../../src' import { Pokemon } from './pokemon.component' import style from './pokemon.island.css' import { injectCSS } from './utils' injectCSS(style) const island = createIsland(Pokemon) island.render({ selector: '[data-island="pokemon"]', replace: true, }) ================================================ FILE: example/src/template.html ================================================ Widgets
================================================ FILE: example/src/utils.ts ================================================ export const injectCSS = (style: string) => { document.head.insertAdjacentHTML('beforeend', ``) } ================================================ FILE: example/tsconfig.json ================================================ { "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact", "allowJs": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "forceConsistentCasingInFileNames": true, "strict": true, "noImplicitOverride": true, "noFallthroughCasesInSwitch": true, "sourceMap": true, "declaration": false, "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true, "importHelpers": true, "target": "es2015", "module": "esnext", "types": ["node"], "lib": ["es2017", "dom", "DOM.Iterable"], "skipLibCheck": true, "skipDefaultLibCheck": true, "noPropertyAccessFromIndexSignature": false, "baseUrl": "." }, "exclude": [ "config/setupTests.ts", "jest.config.ts", "**/*.spec.ts", "**/*.test.ts", "**/*.spec.tsx", "**/*.test.tsx", "**/*.spec.js", "**/*.test.js", "**/*.spec.jsx", "**/*.test.jsx" ], "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"] } ================================================ FILE: example/webpack.config.js ================================================ const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') const TerserPlugin = require('terser-webpack-plugin') module.exports = ({ dev, prod }) => { const isDev = dev === true const isProd = prod === true /** @type { import('webpack').Configuration } */ const config = { mode: isProd ? 'production' : 'development', target: 'web', resolve: { extensions: ['.js', '.json', '.ts', '.tsx'], /** * From the docs to make Webpack compile Preact: * https://preactjs.com/guide/v10/getting-started#aliasing-in-webpack */ alias: { react: 'preact/compat', 'react-dom/test-utils': 'preact/test-utils', 'react-dom': 'preact/compat', // Must be below test-utils 'react/jsx-runtime': 'preact/jsx-runtime', }, }, devServer: { port: 6464, hot: false, }, devtool: false, entry: { 'call-to-action': './src/call-to-action.island.tsx', 'email-subscribe': './src/email-subscribe.island.tsx', pokemon: './src/pokemon.island.tsx', }, output: { path: path.join(__dirname, 'dist/islands'), filename: '[name].island.umd.js', libraryTarget: 'umd', }, module: { rules: [ { test: /\.(js|ts|tsx)$/, exclude: [/node_modules/], use: [ { loader: 'babel-loader', options: { babelrc: false, presets: [ '@babel/preset-typescript', ['@babel/preset-react', { runtime: 'automatic' }], [ '@babel/preset-env', { targets: { node: 16 }, modules: false }, ], ], }, }, ], }, { test: /\.css$/i, use: ['css-loader'], }, { test: /\.(png|jpe?g|gif)$/i, use: [ { loader: 'file-loader', }, ], }, ], }, plugins: [ new HtmlWebpackPlugin({ template: 'src/template.html', /** * Islands are served from /islands in dist so we don't pollute the root domain since these islands are * embedded into websites we do not control. * * In dev mode, we serve islands and the index.html from the root since it's dev mode. For production, * the index.html file is served from the root. */ publicPath: isDev ? '/' : '/islands', filename: isDev ? 'index.html' : '../index.html', }), ], stats: 'errors-warnings', optimization: { minimize: true, minimizer: [new TerserPlugin()], }, } return config } ================================================ FILE: example-react/declaration.d.ts ================================================ declare module '*.css' ================================================ FILE: example-react/package.json ================================================ { "name": "preact-island-react-examples", "version": "0.1.0", "description": "", "source": "src/index.tsx", "main": "dist/index.js", "module": "dist/index.module.js", "umd:main": "dist/index.umd.js", "scripts": { "build": "NODE_OPTIONS=--max-old-space-size=8192 microbundle src/*.island.tsx build --no-sourcemap --output dist/islands --external none --css inline --tsconfig tsconfig.json --alias react=preact/compat,react-dom=preact/compat,react-dom/test-utils=preact/compat,react/jsx-runtime=preact/compat" }, "files": [ "dist" ], "license": "MIT", "devDependencies": { "@types/react": "^17.0.1", "@types/react-dom": "^17.0.1", "clsx": "^1.1.1", "microbundle": "^0.15.0", "react": "^17.0.1", "react-dom": "^17.0.1" } } ================================================ FILE: example-react/src/call-to-action.island.css ================================================ .cta_button { background-color: none; border: none; background-color: #294eab; border-radius: 5px; padding: 10px; font-weight: bold; color: white; cursor: pointer; font-family: inherit; } .cta__modal-dimmer { position: fixed; display: none; z-index: 90; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.6); } .cta__modal-dimmer--visible { display: block; animation: show 0.2s; animation-fill-mode: forwards; } .cta__modal { position: fixed; outline: none; z-index: 100; background-color: white; display: none !important; width: 380px; border-radius: 24px; padding: 34px 21px; left: 50%; top: 50%; transform: translate(-50%, -50%); font-family: inherit; overflow-y: auto; height: 650px; text-align: center; } .cta__modal--visible { display: block !important; animation: show 0.3s; animation-fill-mode: forwards; } .cta__modal img { width: 100%; margin-bottom: 1rem; } ================================================ FILE: example-react/src/call-to-action.react.island.tsx ================================================ import { createIsland } from '../../dist/index.module' import { useState } from 'react' import { createPortal } from 'react-dom' import style from './call-to-action.island.css' import { injectCSS } from './utils' import cx from 'clsx' injectCSS(style) const Widget = ({ backgroundColor }: { backgroundColor?: string }) => { const [isOpen, setIsOpen] = useState(false) return (
{isOpen && createPortal(

Portals work with islands too!

, document.body, )} {isOpen && createPortal(
setIsOpen(false)} />, document.body, )}
) } const island = createIsland(Widget) island.render({ selector: '[data-island="call-to-action"]', }) ================================================ FILE: example-react/src/pokemon.component.tsx ================================================ import { useEffect, useState } from 'react' export const Pokemon = ({ pokemon }: { pokemon: string }) => { const [pokemonData, setPokemonData] = useState(null) const [loading, setLoading] = useState(false) const [error, setError] = useState('') useEffect(() => { if (!pokemon) return setLoading(true) fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon}`) .then((d) => d.json()) .then((d) => { setLoading(false) setPokemonData(d) setError('') }) .catch((e) => setError(e.message || 'Something went wrong!')) }, [pokemon]) if (error) { return
{error}
} if (loading) { return
Loading...
} if (!pokemonData) { return (
Select a pokemon to see info
) } return (
{pokemonData.name}

Name: {pokemonData.name}

Number: {pokemonData.id}

) } ================================================ FILE: example-react/src/pokemon.island.css ================================================ .pokemon__container { border-radius: 5px; border: 1px solid #eae7e7; padding: 1rem; display: inline-block; } .pokemon__image { width: 200px; height: 200px; text-align: center; } .pokemon__info { color: #2b2e34; margin-bottom: 5px; } ================================================ FILE: example-react/src/pokemon.react.island.tsx ================================================ import { createIsland } from '../../dist/index.module' import { Pokemon } from './pokemon.component' import style from './pokemon.island.css' import { injectCSS } from './utils' injectCSS(style) const island = createIsland(Pokemon) island.render({ selector: '[data-island="pokemon"]', }) ================================================ FILE: example-react/src/utils.ts ================================================ export const injectCSS = (style: string) => { document.head.insertAdjacentHTML('beforeend', ``) } ================================================ FILE: example-react/tsconfig.json ================================================ { "compilerOptions": { "target": "es2017", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "strict": true, "forceConsistentCasingInFileNames": true, "module": "ES2015", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "declaration": true, "preserveWatchOutput": true, "jsx": "react-jsx", "jsxFactory": "", "jsxFragmentFactory": "", "noEmit": true }, "include": ["src", "declaration.d.ts"], "exclude": ["**/tests/**"] } ================================================ FILE: jest.config.js ================================================ export default { collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'], preset: 'jest-preset-preact', setupFilesAfterEnv: ['/config/setupTests.js'], testEnvironment: 'jsdom', globals: { 'ts-jest': { isolatedModules: true, }, }, } ================================================ FILE: package.json ================================================ { "name": "preact-island", "version": "1.2.0", "type": "module", "description": "🏝 Create your own slice of paradise on any website.", "source": "src/index.ts", "main": "./dist/index.cjs", "module": "./dist/index.module.js", "exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.modern.js", "require": "./dist/index.cjs", "umd": "./dist/index.umd.js" } }, "unpkg": "./dist/index.umd.js", "types": "./dist/index.d.ts", "scripts": { "build:netlify": "npm run build && cd ./example && npm install && npm run build", "build:netlify:react": "npm run build && cd ./example-react && npm install && npm run build", "build": "microbundle --sourcemap false", "dev:lib": "microbundle watch", "dev:example": "cd example && npm run dev", "dev": "run-p dev:*", "test": "jest" }, "repository": { "type": "git", "url": "git+https://github.com/mwood23/preact-island.git" }, "keywords": [ "preact", "habitat", "shopify", "cms widgets" ], "author": "Marcus Wood", "license": "MIT", "bugs": { "url": "https://github.com/mwood23/preact-island/issues" }, "homepage": "https://github.com/mwood23/preact-island#readme", "eslintConfig": { "parser": "@typescript-eslint/parser", "extends": [ "preact", "plugin:@typescript-eslint/recommended" ], "ignorePatterns": [ "build/" ] }, "dependencies": {}, "devDependencies": { "@types/jest": "^27.5.1", "@testing-library/jest-dom": "^5.16.4", "@testing-library/user-event": "^14.2.0", "microbundle": "^0.15.0", "eslint": "^8.15.0", "eslint-config-preact": "^1.3.0", "prettier": "^2.6.2", "jest": "^27.2.5", "preact": "^10.7.2", "preact-render-to-string": "^5.1.4", "npm-run-all": "^4.1.5", "@testing-library/preact": "^2.0.1", "jest-preset-preact": "^4.0.2", "typescript": "^4.6.4" }, "peerDependencies": { "preact": ">=10" } } ================================================ FILE: src/index.ts ================================================ export * from './island' export * from './island-web-components' ================================================ FILE: src/island-web-components.tsx ================================================ import { ComponentType, VNode } from 'preact' import { createPortal, FC, useEffect, useMemo } from 'preact/compat' import { createIsland, Island } from './island' export type InitialPropsWebComponent = { [x: string]: any } export const createIslandWebComponent =

( /** * Must be a valid web component element name. See spec for more details: * https://html.spec.whatwg.org/#valid-custom-element-name */ elementName: string, /** * Component you would like to render inside of the web component */ widget: ComponentType

, ) => { if (customElements.get(elementName) == null) { class Element extends HTMLElement { constructor() { super() // Create a shadow root this.attachShadow({ mode: 'open' }) } } customElements.define(elementName, Element) } const island = createIsland(widget) return { ...island, render: ( args: Omit< Parameters['render']>[0], 'replace' | 'clean' | 'inline' >, ) => island.render({ elementName, ...args }), injectStyles: (style: string) => { island._roots.forEach((root) => { const styleElement = document.createElement('style') styleElement.innerHTML = style // @ts-ignore root.parentNode.prepend(styleElement) }) }, } } export const WebComponentPortal: FC<{ name: string container?: Element children: VNode<{}> style?: string | HTMLStyleElement }> = ({ name, container = document.body, children, style }) => { const portalTarget = useMemo(() => { if (customElements.get(name) == null) { class Element extends HTMLElement { constructor() { super() // Create a shadow root const shadowRoot = this.attachShadow({ mode: 'open' }) if (!style) return if (style instanceof HTMLStyleElement) { shadowRoot.prepend(style) } else { const styleElement = document.createElement('style') styleElement.innerHTML = style shadowRoot.prepend(styleElement) } } } customElements.define(name, Element) } const customElement = document.createElement(name) return container.appendChild(customElement) }, [container, style]) useEffect(() => { return () => { portalTarget.remove() } }, [portalTarget]) // @ts-ignore - Internal types wrong return createPortal(children, portalTarget.shadowRoot) } ================================================ FILE: src/island.ts ================================================ import { getHostElements, mount, RootFragment, renderIsland } from './lib' import { render, ComponentType } from 'preact' export type InitialProps = { [x: string]: any } export type Island

= { /** * A WeakMap that yields the mutation observers associated with a particular root. Used for cleaning up observers * on destroy. */ _rootsToObservers: WeakMap /** * An array of the root fragments (a fake DOM element) containing one or more * DOM nodes, which can then be passed as the `parent` argument to Preact's `render()` method. */ _roots: RootFragment[] /** * A reference to the executed script that called `createIsland`. This is used for listening to prop * changes on that script and causing rerenders of the island. */ _executedScript: HTMLOrSVGScriptElement | null /** * Renders the created island at the given selector. Calling multiple times appends elements at the given selectors. */ render: (props: { /** * A query selector target to create the widget. This is ignored if inline is passed or if a `data-mount-in` attribute * is appended onto the executed script. * * @example '[data-island="widget"]' */ selector?: string /** * If true, removes all children of the element before rendering the component. * * @default false * * @example * ```html *

*
some other content
*
some other content
*
some other content
*
* ``` * * // turns into * * ```html *
*
your-widget
*
* ``` * * // turns into * * ```html *
your-widget
* ``` */ replace?: boolean /** * Renders the widget at the current position of the script in the HTML document. * * @default false * * @example * ```html *
*
some content here
* *
some content here
*
* ``` * * // turns into * * ```html *
*
some content here
* *
your widget
*
some content here
*
* ``` */ inline?: boolean /** * Initial props to pass to the component. These props do not cause updates to the island if changed. Use `createIsland().rerender` instead. */ initialProps?: Partial

/** * A valid selector to a script tag located in the HTML document with a type of either `text/props` or `application/json` * containing props to pass into the component. If there are multiple scripts found with the selector, all props are merged with * the last script found taking priority. */ propsSelector?: string /** * Passed in if using for web components */ elementName?: string }) => void /** * Contains the current props used to render the island. */ props: P /** * Triggers a rerenders of the island with the new props given. */ rerender: (props: P) => void /** * Destroys all instances of the island on the page and disconnects any associated observers. */ destroy: () => void } export const createIsland =

( widget: ComponentType

, ) => { const island: Island

= { _rootsToObservers: new WeakMap(), _roots: [], _executedScript: document.currentScript, // @ts-ignore props: {}, render: ({ selector, clean = false, replace = false, inline = false, initialProps = {}, propsSelector, elementName, }) => { let rendered = false const load = () => { /** * We listen for multiple events to render so soon as we do it once * successfully we break early for others. */ if (rendered === true) return const hostElements = getHostElements({ selector, inline, elementName, }) // Do nothing if no host elements returned if (hostElements.length === 0) return const { rootFragments } = mount

({ island, widget, clean, hostElements, replace, // @ts-ignore Not sure how to fix this error initialProps, propsSelector, }) island._roots = island._roots.concat(rootFragments) rendered = true } load() document.addEventListener('DOMContentLoaded', load) document.addEventListener('load', load) }, rerender: (newProps) => { island._roots.forEach((rootFragment) => { renderIsland({ island, widget, rootFragment, props: { ...island.props, ...newProps }, }) }) }, destroy: () => { island._roots.forEach((rootFragment) => { island._rootsToObservers.get(rootFragment)?.disconnect() render(null, rootFragment) }) }, } return island } ================================================ FILE: src/lib.ts ================================================ import { ComponentType, h, render } from 'preact' import { InitialProps, Island } from './island' type HostElement = HTMLElement | ShadowRoot export const isInShadow = (node: HostElement | HTMLOrSVGScriptElement) => { return node.getRootNode() instanceof ShadowRoot } export const isShadowRoot = (x: unknown): x is ShadowRoot => { return x instanceof ShadowRoot } export const formatProp = (str: string) => { return `${str.charAt(0).toLowerCase()}${str.slice(1)}` } export const getPropsFromElement = ( element: HostElement | HTMLOrSVGScriptElement, ) => { // In a shadow dom we replace the host element because it's within the shadow root. However, // we want the props of the autonomous custom element. const targetElement = isInShadow(element) ? (element.getRootNode() as any).host : element const { dataset } = targetElement const props: { [x: string]: any } = {} for (var d in dataset) { // We don't pull props for inherited attributes if (dataset.hasOwnProperty(d) === false) return // data-prop or data-props works! const propName = formatProp(d.split(/(props?)/).pop() || '') if (propName) { props[propName] = dataset[d] } } return props } export const isValidPropsScript = (element: Element) => { return ( // element.tagName.toLowerCase() === 'script' && ['text/props', 'application/json'].includes( element.getAttribute('type') || '', ) ) } export const getInteriorPropsScriptsForElement = (element: HostElement) => { // getElementsByTagName does not exist on shadow roots and within a shadow root // the caller can't place in props scripts if (isShadowRoot(element)) return [] return Array.from(element.getElementsByTagName('script')).filter( isValidPropsScript, ) } export const getPropsScriptsBySelector = (selector: string) => { return Array.from(document.querySelectorAll(selector)).filter( isValidPropsScript, // Checked by filter call ) as HTMLOrSVGScriptElement[] } export const getPropsFromScripts = (scripts: HTMLOrSVGScriptElement[]) => { let interiorScriptProps: any = {} scripts.forEach((script) => { // Swallow any potential errors so we don't throw on someone else's page try { interiorScriptProps = { ...interiorScriptProps, ...JSON.parse(script.innerHTML), } } catch (e: any) {} }) return interiorScriptProps } /** * Get the props from a host element's data attributes * @param {Element} The host element * @return {Object} props object to be passed to the component */ export const generateHostElementProps =

( island: Island

, element: HostElement, initialProps = {}, propsSelector: string | undefined | null, ): P => { const elementProps = getPropsFromElement(element) const currentScriptProps = island._executedScript ? getPropsFromElement(island._executedScript) : {} const interiorScriptProps = getPropsFromScripts( getInteriorPropsScriptsForElement(element), ) const propsSelectorProps = propsSelector ? getPropsFromScripts(getPropsScriptsBySelector(propsSelector)) : {} return { ...initialProps, ...elementProps, ...currentScriptProps, ...propsSelectorProps, ...interiorScriptProps, } } export const getHostElements = ({ selector, inline, elementName, }: { selector?: string inline: boolean /** * Passed if targeting web components so that mount in can create web components inside of the host elements */ elementName?: string }): HostElement[] => { const currentScript = document.currentScript if (inline && currentScript?.parentNode) { // @ts-ignore Not sure on this one return [currentScript.parentNode] } // Next, try to get the selector from the current script const maybeSelector = currentScript?.dataset.mountIn if (maybeSelector) { return Array.from( document.querySelectorAll(maybeSelector), ).map((n) => { if (elementName != null) { const targetElement = document.createElement(elementName) const node = n.appendChild(targetElement) return node.shadowRoot != null ? node.shadowRoot : node } return n }) } if (selector) { return Array.from(document.querySelectorAll(selector)).map( (n) => (n.shadowRoot != null ? n.shadowRoot : n), ) } return [] } /** * A Preact 11+ implementation of the `replaceNode` parameter from Preact 10. * * This creates a "Persistent Fragment" (a fake DOM element) containing one or more * DOM nodes, which can then be passed as the `parent` argument to Preact's `render()` method. * * Lifted from: https://gist.github.com/developit/f4c67a2ede71dc2fab7f357f39cff28c */ export type RootFragment = any export function createRootFragment( parent: HostElement, replaceNode: HostElement | HostElement[], ): RootFragment { replaceNode = ([] as HostElement[]).concat(replaceNode) var s = replaceNode[replaceNode.length - 1].nextSibling function insert(c: HTMLElement, r: HTMLElement) { parent.insertBefore(c, r || s) } // Mutating the parent to add a preact property // @ts-expect-error We're mutating the parent to add these properties for Preact return (parent.__k = { nodeType: 1, parentNode: parent, firstChild: replaceNode[0], childNodes: replaceNode, insertBefore: insert, appendChild: insert, removeChild: function (c: HTMLElement) { parent.removeChild(c) }, }) } export const watchForPropChanges =

({ island, hostElement, initialProps, onNewProps, propsSelector, }: { island: Island

hostElement: HostElement initialProps: any onNewProps: (props: P) => void propsSelector: string | undefined | null }) => { const observer = new MutationObserver(function (mutations) { mutations.forEach(function () { onNewProps( generateHostElementProps( island, hostElement, initialProps, propsSelector, ), ) }) }) const config = { attributes: true, childList: true, characterData: true } if (island._executedScript) { observer.observe(island._executedScript, config) } getInteriorPropsScriptsForElement(hostElement).forEach((script) => { observer.observe(script, { ...config, subtree: true }) }) if (propsSelector) { getPropsScriptsBySelector(propsSelector).forEach((script) => { observer.observe(script, { ...config, subtree: true }) }) } /** * If the host element is a shadow root we want to observe on the host of it. * * Example: * * #shadow-root (open) * * * We want to observe the custom autonomous element, not the shadow root! */ observer.observe( isShadowRoot(hostElement) ? hostElement.host! : hostElement, config, ) return observer } export const renderIsland =

({ island, widget, rootFragment, props, }: { island: Island

widget: ComponentType

rootFragment: RootFragment props: P }) => { island.props = props render(h(widget, props), rootFragment) } export const mount =

({ island, widget, hostElements, clean, replace, initialProps, propsSelector, }: { island: Island

widget: ComponentType

hostElements: Array clean: boolean replace: boolean initialProps: P propsSelector?: string }) => { const rootFragments: any = [] hostElements.forEach((hostElement) => { const props = generateHostElementProps

( island, hostElement, initialProps, propsSelector, ) if (clean) { hostElement.replaceChildren() } let rootFragment: any if (replace) { rootFragment = createRootFragment( hostElement.parentElement || document.body, hostElement, ) } else { const renderNode = document.createElement('div') hostElement.appendChild(renderNode) rootFragment = createRootFragment(hostElement, renderNode) } rootFragments.push(rootFragment) renderIsland({ island, widget, rootFragment, props }) const observer = watchForPropChanges

({ island, hostElement, initialProps, onNewProps: (newProps) => { renderIsland({ island, widget, rootFragment, props: newProps }) }, propsSelector, }) island._rootsToObservers.set(rootFragment, observer) }) return { rootFragments } } ================================================ FILE: src/tests/helpers/getById.ts ================================================ export const getById = (x: string) => { const element = document.getElementById(x) if (element == null) { throw new Error(`Could not find element with id: ${x}!`) } return element } ================================================ FILE: src/tests/helpers/inlineScript.tsx ================================================ import { createIsland } from '../../island' import { mount, getHostElements, formatProp, getInteriorPropsScriptsForElement, getPropsFromScripts, generateHostElementProps, getPropsFromElement, createRootFragment, watchForPropChanges, isValidPropsScript, renderIsland, isInShadow, isShadowRoot, } from '../../lib' import { h, FunctionComponent } from 'preact' /** * Use this helper when you want to test what it's like to use an inline script to use preact-island. This imports and stubs everything the Babel transforms under * the hood for Jest to run the things. I'm not sure a better way to test this unless it's importing the dist files but then that requires a build step every time * we run tests. */ export const InlineScript: FunctionComponent<{ widget: any renderCode: string id?: string }> = ({ widget, renderCode, ...rest }) => { return ( ) } ================================================ FILE: src/tests/island.props.test.tsx ================================================ import { h } from 'preact' import { createIsland } from '../island' import { useState } from 'preact/hooks' import { render, waitFor } from '@testing-library/preact' import userEvent from '@testing-library/user-event' import { InlineScript } from './helpers/inlineScript' import { getById } from './helpers/getById' const Widget = (props: any) => { const [toggle, setToggle] = useState(false) return (

{toggle ? toggled : null} {JSON.stringify(props)}
) } it('should render at the given selector and trigger a rerender (not remount) when new props are passed to the host', async () => { const user = userEvent.setup() const r = render(
, ) const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', }) await waitFor(() => expect(r.getByTestId('widgetProps').textContent).toEqual( '{"island":"island","testid":"island-host","test":"bananas"}', ), ) /** * We set some local state to the rendered widget to make sure when we update the props that the localized state * continues to exist! */ const buttonToggleNode = r.getByTestId('toggleButton') user.click(buttonToggleNode) await waitFor(() => expect(r.getByTestId('toggled')).toBeInTheDocument()) const islandHost = r.getByTestId('island-host') islandHost.dataset.propTest = 'apples' // By asserting that toggled is still in the document it shows we didn't accidentally remount // the component on prop changes await waitFor(() => expect(r.getByTestId('toggled')).toBeInTheDocument()) await waitFor(() => expect(r.getByTestId('widgetProps').textContent).toEqual( '{"island":"island","testid":"island-host","test":"apples"}', ), ) }) it('should render at the given selector and trigger a rerender (not remount) when rerender is called with new props', async () => { const user = userEvent.setup() const r = render(
, ) const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', }) await waitFor(() => expect(r.getByTestId('widgetProps').textContent).toEqual( '{"island":"island","testid":"island-host","test":"bananas"}', ), ) /** * We set some local state to the rendered widget to make sure when we update the props that the localized state * continues to exist! */ const buttonToggleNode = r.getByTestId('toggleButton') user.click(buttonToggleNode) await waitFor(() => expect(r.getByTestId('toggled')).toBeInTheDocument()) island.rerender({ test: 'apples' }) // By asserting that toggled is still in the document it shows we didn't accidentally remount // the component on prop changes await waitFor(() => expect(r.getByTestId('toggled')).toBeInTheDocument()) await waitFor(() => expect(r.getByTestId('widgetProps').textContent).toEqual( '{"island":"island","testid":"island-host","test":"apples"}', ), ) }) it('should render at the given selector and trigger a rerender (not remount) when new props are passed to the child script props tag', async () => { const user = userEvent.setup() const r = render(
, ) const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', }) await waitFor(() => expect(r.getByTestId('widgetProps').textContent).toEqual( '{"island":"island","testid":"island-host","test":"bananas"}', ), ) /** * We set some local state to the rendered widget to make sure when we update the props that the localized state * continues to exist! */ const buttonToggleNode = r.getByTestId('toggleButton') user.click(buttonToggleNode) await waitFor(() => expect(r.getByTestId('toggled')).toBeInTheDocument()) const scriptProps = r.getByTestId('script-props') scriptProps.innerHTML = '{"test": "apples"}' // By asserting that toggled is still in the document it shows we didn't accidentally remount // the component on prop changes await waitFor(() => expect(r.getByTestId('toggled')).toBeInTheDocument()) await waitFor(() => expect(r.getByTestId('widgetProps').textContent).toEqual( '{"island":"island","testid":"island-host","test":"apples"}', ), ) }) it('should render at the given selector and trigger a rerender (not remount) when new props are passed to the targeted props script', async () => { const user = userEvent.setup() const r = render(
, ) const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', propsSelector: '[data-island-props="test-island"]', }) await waitFor(() => expect(r.getByTestId('widgetProps').textContent).toEqual( '{"island":"island","testid":"island-host","test":"bananas"}', ), ) /** * We set some local state to the rendered widget to make sure when we update the props that the localized state * continues to exist! */ const buttonToggleNode = r.getByTestId('toggleButton') user.click(buttonToggleNode) await waitFor(() => expect(r.getByTestId('toggled')).toBeInTheDocument()) const scriptProps = getById('inline-script-test') scriptProps.innerHTML = '{"test": "apples"}' // By asserting that toggled is still in the document it shows we didn't accidentally remount // the component on prop changes await waitFor(() => expect(r.getByTestId('toggled')).toBeInTheDocument()) await waitFor(() => expect(r.getByTestId('widgetProps').textContent).toEqual( '{"island":"island","testid":"island-host","test":"apples"}', ), ) }) it('should render at the given selector and trigger a rerender (not remount) when new props are passed to the injected script', async () => { const user = userEvent.setup() const r = render(
, ) await waitFor(() => expect(r.getByTestId('widgetProps').textContent).toEqual( '{"island":"island","testid":"island-host","test":"bananas"}', ), ) /** * We set some local state to the rendered widget to make sure when we update the props that the localized state * continues to exist! */ const buttonToggleNode = r.getByTestId('toggleButton') user.click(buttonToggleNode) await waitFor(() => expect(r.getByTestId('toggled')).toBeInTheDocument()) const scriptProps = getById('inline-script') scriptProps.dataset.test = 'apples' // By asserting that toggled is still in the document it shows we didn't accidentally remount // the component on prop changes await waitFor(() => expect(r.getByTestId('toggled')).toBeInTheDocument()) await waitFor(() => expect(r.getByTestId('widgetProps').textContent).toEqual( '{"island":"island","testid":"island-host","test":"apples"}', ), ) }) ================================================ FILE: src/tests/island.selector.test.tsx ================================================ import { h } from 'preact' import { createIsland } from '../island' import { render, waitFor, within } from '@testing-library/preact' import { InlineScript } from './helpers/inlineScript' const Widget = (props: any) => { return
{JSON.stringify(props)}
} it('should render at the given selector', async () => { const r = render(
) await waitFor(() => expect(r.queryByTestId('widget')).not.toBeInTheDocument()) const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', }) await waitFor(() => expect(r.container).toMatchInlineSnapshot(`
{"island":"island"}
`), ) }) it('should render inline if prop is passed and take highest priority', async () => { const r = render(
, ) await waitFor(() => expect(r.getByTestId('parent-node').lastChild).toMatchInlineSnapshot(`
{"testid":"parent-node"}
`), ) }) it('should render using the data-mount-in prop and take priority over a selector if passed', async () => { const r = render(
, ) await waitFor(() => expect( within(r.getByTestId('selector')).queryByTestId('widget'), ).not.toBeInTheDocument(), ) await waitFor(() => expect( within(r.getByTestId('inlineScriptMountIn')).queryByTestId('widget'), ).toBeInTheDocument(), ) }) it('should pass down default props', async () => { const r = render(
) const initialProps = { apples: 'yes', bananas: 1, kiwis: true, } const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', initialProps, }) await waitFor(() => expect(r.getByTestId('widget').innerHTML).toEqual( JSON.stringify({ ...initialProps, island: 'island' }), ), ) }) it('should render twice if multiple nodes are found from the selector', async () => { const r = render(
, ) const initialProps = { apples: 'yes', bananas: 1, kiwis: true, } const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', initialProps, }) // NOTE: Notice there is a different between this one and the other test. // This one render inside of two data-island elements whereas the other // renders twice inside of the same data widget host element! await waitFor(() => expect(r.container).toMatchInlineSnapshot(`
{"apples":"yes","bananas":1,"kiwis":true,"island":"island"}
{"apples":"yes","bananas":1,"kiwis":true,"island":"island"}
`), ) }) it('should render multiple times if render is called twice', async () => { const r = render(
) const initialProps = { apples: 'yes', bananas: 1, kiwis: true, } const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', initialProps, }) island.render({ selector: '[data-island="island"]', initialProps, }) await waitFor(() => expect(r.container).toMatchInlineSnapshot(`
{"apples":"yes","bananas":1,"kiwis":true,"island":"island"}
{"apples":"yes","bananas":1,"kiwis":true,"island":"island"}
`), ) }) it('should replace the node at given selector if prop is given', async () => { const r = render(
) const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', replace: true, }) await waitFor(() => expect(r.container).toMatchInlineSnapshot(`
{"island":"island"}
`), ) }) it('should destroy mounted islands when destroy called', async () => { const r = render(
) const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', }) await waitFor(() => expect(r.container).toMatchInlineSnapshot(`
{"island":"island"}
`), ) island.destroy() await waitFor(() => expect(r.container).toMatchInlineSnapshot(`
`), ) }) it('should destroy mounted islands when destroy called with replace: true', async () => { const r = render(
) const island = createIsland(Widget) island.render({ selector: '[data-island="island"]', replace: true, }) await waitFor(() => expect(r.container).toMatchInlineSnapshot(`
{"island":"island"}
`), ) island.destroy() await waitFor(() => expect(r.container).toMatchInlineSnapshot(`
`)) }) ================================================ FILE: tsconfig.json ================================================ { "compilerOptions": { "target": "es2017", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "strict": true, "forceConsistentCasingInFileNames": true, "module": "ES2015", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "declaration": true, "preserveWatchOutput": true, "jsx": "react", "jsxFactory": "h", "jsxFragmentFactory": "" }, "include": ["src"], "exclude": ["**/tests/**"] }