Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@ The `SuperActionProvider` component below listens for click events. React develo
```tsx
import React from "react";
import ReactDOM from "react-dom/client";

import { SuperAction } from "@w-lfpup/superaction";
import { SuperActionProvider } from "@w-lfpup/react-superaction";
import { Counter } from "./counter.js";

let rootEl = document.querySelector("#root")!;
const root = ReactDOM.createRoot(rootEl);

let eventNames: string[] = ["click"];
let _superAction = new SuperAction({
host: rootEl,
eventNames: ["click"],
connected: true,
infix: "-", // react-safe-html-attributes
});

const root = ReactDOM.createRoot(rootEl);
root.render(
<SuperActionProvider eventNames={eventNames}>
<SuperActionProvider target={rootEl}>
<Counter />
</SuperActionProvider>,
);
Expand Down
4 changes: 1 addition & 3 deletions dist/provider.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { ReactNode } from "react";
import React from "react";
interface ProviderProps {
eventNames: string[];
children: ReactNode;
host?: EventTarget;
target?: EventTarget;
target: EventTarget;
}
export declare function SuperActionProvider(props: ProviderProps): React.JSX.Element;
export {};
18 changes: 5 additions & 13 deletions dist/provider.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import React, { useEffect, useState } from "react";
import { SuperAction, ActionEvent, } from "@w-lfpup/superaction";
import { ActionEvent } from "@w-lfpup/superaction";
import { SuperContext } from "./context.js";
export function SuperActionProvider(props) {
let { eventNames, children, host = document, target } = props;
let { children, target } = props;
let [value, setValue] = useState(undefined);
useEffect(function () {
let superAction = new SuperAction({
infix: "-",
host,
target,
eventNames,
});
function cb(e) {
if (e instanceof ActionEvent)
setValue(e.action);
}
superAction.connect();
host.addEventListener("#action", cb);
target.addEventListener("#action", cb);
return function () {
superAction.disconnect();
host.removeEventListener("#action", cb);
target.removeEventListener("#action", cb);
};
}, [host, target, eventNames]);
}, [target]);
return (React.createElement(SuperContext.Provider, { value: value }, children));
}
49 changes: 23 additions & 26 deletions examples/counter/bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -30244,18 +30244,6 @@ function requireClient () {
var clientExports = requireClient();
var ReactDOM = /*@__PURE__*/getDefaultExportFromCjs(clientExports);

const SuperContext = reactExports.createContext(undefined);

function useAction(cb, args) {
let action = reactExports.useContext(SuperContext);
let [prevAction, setPrevAction] = reactExports.useState(undefined);
if (action === prevAction)
return;
setPrevAction(action);
if (action)
cb(action);
}

class ActionEvent extends Event {
action;
constructor(action, eventInit) {
Expand Down Expand Up @@ -30322,27 +30310,31 @@ function dispatch(params, event) {
}
}

const SuperContext = reactExports.createContext(undefined);

function useAction(cb, args) {
let action = reactExports.useContext(SuperContext);
let [prevAction, setPrevAction] = reactExports.useState(undefined);
if (action === prevAction)
return;
setPrevAction(action);
if (action)
cb(action);
}

function SuperActionProvider(props) {
let { eventNames, children, host = document, target } = props;
let { children, target } = props;
let [value, setValue] = reactExports.useState(undefined);
reactExports.useEffect(function () {
let superAction = new SuperAction({
infix: "-",
host,
target,
eventNames,
});
function cb(e) {
if (e instanceof ActionEvent)
setValue(e.action);
}
superAction.connect();
host.addEventListener("#action", cb);
target.addEventListener("#action", cb);
return function () {
superAction.disconnect();
host.removeEventListener("#action", cb);
target.removeEventListener("#action", cb);
};
}, [host, target, eventNames]);
}, [target]);
return (React.createElement(SuperContext.Provider, { value: value }, children));
}

Expand All @@ -30361,10 +30353,15 @@ function Counter() {
React.createElement("p", null, count)));
}

let eventNames = ["click"];
let rootEl = document.querySelector("#root");
if (rootEl) {
new SuperAction({
host: rootEl,
eventNames: ["click"],
connected: true,
infix: "-", // react safe
});
const root = ReactDOM.createRoot(rootEl);
root.render(React.createElement(SuperActionProvider, { eventNames: eventNames },
root.render(React.createElement(SuperActionProvider, { target: rootEl },
React.createElement(Counter, null)));
}
10 changes: 8 additions & 2 deletions examples/counter/root.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { SuperAction } from "@w-lfpup/superaction";
import { SuperActionProvider } from "../../dist/mod.js";
import { Counter } from "./counter.js";
let eventNames = ["click"];
let rootEl = document.querySelector("#root");
if (rootEl) {
let _superAction = new SuperAction({
host: rootEl,
eventNames: ["click"],
connected: true,
infix: "-", // react safe
});
const root = ReactDOM.createRoot(rootEl);
root.render(React.createElement(SuperActionProvider, { eventNames: eventNames },
root.render(React.createElement(SuperActionProvider, { target: rootEl },
React.createElement(Counter, null)));
}
12 changes: 9 additions & 3 deletions examples/counter/root.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { SuperAction } from "@w-lfpup/superaction";
import { SuperActionProvider } from "../../dist/mod.js";
import { Counter } from "./counter.js";

let eventNames: string[] = ["click"];

let rootEl = document.querySelector("#root");
if (rootEl) {
let _superAction = new SuperAction({
host: rootEl,
eventNames: ["click"],
connected: true,
infix: "-", // react safe
});

const root = ReactDOM.createRoot(rootEl);
root.render(
<SuperActionProvider eventNames={eventNames}>
<SuperActionProvider target={rootEl}>
<Counter />
</SuperActionProvider>,
);
Expand Down
49 changes: 23 additions & 26 deletions examples/form/bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -30244,18 +30244,6 @@ function requireClient () {
var clientExports = requireClient();
var ReactDOM = /*@__PURE__*/getDefaultExportFromCjs(clientExports);

const SuperContext = reactExports.createContext(undefined);

function useAction(cb, args) {
let action = reactExports.useContext(SuperContext);
let [prevAction, setPrevAction] = reactExports.useState(undefined);
if (action === prevAction)
return;
setPrevAction(action);
if (action)
cb(action);
}

class ActionEvent extends Event {
action;
constructor(action, eventInit) {
Expand Down Expand Up @@ -30322,27 +30310,31 @@ function dispatch(params, event) {
}
}

const SuperContext = reactExports.createContext(undefined);

function useAction(cb, args) {
let action = reactExports.useContext(SuperContext);
let [prevAction, setPrevAction] = reactExports.useState(undefined);
if (action === prevAction)
return;
setPrevAction(action);
if (action)
cb(action);
}

function SuperActionProvider(props) {
let { eventNames, children, host = document, target } = props;
let { children, target } = props;
let [value, setValue] = reactExports.useState(undefined);
reactExports.useEffect(function () {
let superAction = new SuperAction({
infix: "-",
host,
target,
eventNames,
});
function cb(e) {
if (e instanceof ActionEvent)
setValue(e.action);
}
superAction.connect();
host.addEventListener("#action", cb);
target.addEventListener("#action", cb);
return function () {
superAction.disconnect();
host.removeEventListener("#action", cb);
target.removeEventListener("#action", cb);
};
}, [host, target, eventNames]);
}, [target]);
return (React.createElement(SuperContext.Provider, { value: value }, children));
}

Expand All @@ -30365,10 +30357,15 @@ function Form() {
React.createElement("pre", null, formAsJSON)));
}

let eventNames = ["submit"];
let rootEl = document.querySelector("#root");
if (rootEl) {
new SuperAction({
host: rootEl,
eventNames: ["submit"],
connected: true,
infix: "-", // react safe
});
const root = ReactDOM.createRoot(rootEl);
root.render(React.createElement(SuperActionProvider, { eventNames: eventNames },
root.render(React.createElement(SuperActionProvider, { target: rootEl },
React.createElement(Form, null)));
}
10 changes: 8 additions & 2 deletions examples/form/root.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { SuperAction } from "@w-lfpup/superaction";
import { SuperActionProvider } from "../../dist/mod.js";
import { Form } from "./form.js";
let eventNames = ["submit"];
let rootEl = document.querySelector("#root");
if (rootEl) {
let _superAction = new SuperAction({
host: rootEl,
eventNames: ["submit"],
connected: true,
infix: "-", // react safe
});
const root = ReactDOM.createRoot(rootEl);
root.render(React.createElement(SuperActionProvider, { eventNames: eventNames },
root.render(React.createElement(SuperActionProvider, { target: rootEl },
React.createElement(Form, null)));
}
12 changes: 9 additions & 3 deletions examples/form/root.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { SuperAction } from "@w-lfpup/superaction";
import { SuperActionProvider } from "../../dist/mod.js";
import { Form } from "./form.js";

let eventNames: string[] = ["submit"];

let rootEl = document.querySelector("#root");
if (rootEl) {
let _superAction = new SuperAction({
host: rootEl,
eventNames: ["submit"],
connected: true,
infix: "-", // react safe
});

const root = ReactDOM.createRoot(rootEl);
root.render(
<SuperActionProvider eventNames={eventNames}>
<SuperActionProvider target={rootEl}>
<Form />
</SuperActionProvider>,
);
Expand Down
Loading