mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Updated signup form options to react to changes (#16854)
refs https://github.com/TryGhost/Team/issues/3293
This commit is contained in:
parent
bed7479cc2
commit
86499aaf01
3 changed files with 40 additions and 14 deletions
|
@ -1,15 +1,18 @@
|
||||||
import React, {ComponentProps} from 'react';
|
import React, {ComponentProps} from 'react';
|
||||||
import pages, {Page, PageName} from './pages';
|
import pages, {Page, PageName} from './pages';
|
||||||
import {AppContextProvider, SignupFormOptions} from './AppContext';
|
import {AppContextProvider} from './AppContext';
|
||||||
import {ContentBox} from './components/ContentBox';
|
import {ContentBox} from './components/ContentBox';
|
||||||
import {Frame} from './components/Frame';
|
import {Frame} from './components/Frame';
|
||||||
import {setupGhostApi} from './utils/api';
|
import {setupGhostApi} from './utils/api';
|
||||||
|
import {useOptions} from './utils/options';
|
||||||
|
|
||||||
type AppProps = {
|
type AppProps = {
|
||||||
options: SignupFormOptions;
|
scriptTag: HTMLElement;
|
||||||
};
|
};
|
||||||
|
|
||||||
const App: React.FC<AppProps> = ({options}) => {
|
const App: React.FC<AppProps> = ({scriptTag}) => {
|
||||||
|
const options = useOptions(scriptTag);
|
||||||
|
|
||||||
const [page, setPage] = React.useState<Page>({
|
const [page, setPage] = React.useState<Page>({
|
||||||
name: 'FormPage',
|
name: 'FormPage',
|
||||||
data: {}
|
data: {}
|
||||||
|
|
|
@ -2,7 +2,6 @@ import App from './App.tsx';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom/client';
|
import ReactDOM from 'react-dom/client';
|
||||||
import {ROOT_DIV_CLASS} from './utils/constants';
|
import {ROOT_DIV_CLASS} from './utils/constants';
|
||||||
import {SignupFormOptions} from './AppContext.ts';
|
|
||||||
|
|
||||||
function getScriptTag(): HTMLElement {
|
function getScriptTag(): HTMLElement {
|
||||||
let scriptTag = document.currentScript as HTMLElement | null;
|
let scriptTag = document.currentScript as HTMLElement | null;
|
||||||
|
@ -45,18 +44,9 @@ function init() {
|
||||||
const scriptTag = getScriptTag();
|
const scriptTag = getScriptTag();
|
||||||
const root = getRootDiv(scriptTag);
|
const root = getRootDiv(scriptTag);
|
||||||
|
|
||||||
const options: SignupFormOptions = {
|
|
||||||
title: scriptTag.dataset.title || undefined,
|
|
||||||
description: scriptTag.dataset.description || undefined,
|
|
||||||
logo: scriptTag.dataset.logo || undefined,
|
|
||||||
color: scriptTag.dataset.color || undefined,
|
|
||||||
site: scriptTag.dataset.site || window.location.origin,
|
|
||||||
labels: scriptTag.dataset.labels ? scriptTag.dataset.labels.split(',') : []
|
|
||||||
};
|
|
||||||
|
|
||||||
ReactDOM.createRoot(root).render(
|
ReactDOM.createRoot(root).render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<App options={options} />
|
<App scriptTag={scriptTag} />
|
||||||
</React.StrictMode>
|
</React.StrictMode>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
33
ghost/signup-form/src/utils/options.tsx
Normal file
33
ghost/signup-form/src/utils/options.tsx
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import React from 'react';
|
||||||
|
import {SignupFormOptions} from '../AppContext';
|
||||||
|
|
||||||
|
export function useOptions(scriptTag: HTMLElement) {
|
||||||
|
const buildOptions = React.useCallback(() => ({
|
||||||
|
title: scriptTag.dataset.title || undefined,
|
||||||
|
description: scriptTag.dataset.description || undefined,
|
||||||
|
logo: scriptTag.dataset.logo || undefined,
|
||||||
|
color: scriptTag.dataset.color || undefined,
|
||||||
|
site: scriptTag.dataset.site || window.location.origin,
|
||||||
|
labels: scriptTag.dataset.labels ? scriptTag.dataset.labels.split(',') : []
|
||||||
|
}), [scriptTag]);
|
||||||
|
|
||||||
|
const [options, setOptions] = React.useState<SignupFormOptions>(buildOptions());
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const observer = new MutationObserver((mutationList) => {
|
||||||
|
if (mutationList.some(mutation => mutation.type === 'attributes')) {
|
||||||
|
setOptions(buildOptions());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
observer.observe(scriptTag, {
|
||||||
|
attributes: true
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
observer.disconnect();
|
||||||
|
};
|
||||||
|
}, [scriptTag, buildOptions]);
|
||||||
|
|
||||||
|
return options;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue