Ghost/apps/announcement-bar/src/index.js
Daniel Lockyer 7a124a6901 Moved announcement-bar to apps/
refs https://github.com/TryGhost/Toolbox/issues/594

- we're moving all the standalone apps to a separate folder to keep them
  out of the core code and easier to find
2023-06-14 12:28:55 +02:00

60 lines
1.3 KiB
JavaScript

import React from 'react';
import ReactDOM from 'react-dom';
import {App} from './App';
const ROOT_DIV_ID = 'announcement-bar-root';
function addRootDiv() {
if (document.getElementById(ROOT_DIV_ID)) {
return;
}
const elem = document.createElement('div');
elem.id = ROOT_DIV_ID;
document.body.prepend(elem);
}
function getSiteData() {
/**
* @type {HTMLElement}
*/
const scriptTag = document.querySelector('script[data-announcement-bar]');
if (scriptTag) {
const apiUrl = scriptTag.dataset.apiUrl;
return {apiUrl, previewData: getPreviewData(scriptTag)};
}
return {};
}
function getPreviewData(scriptTag) {
if (scriptTag.dataset.preview) {
const announcement = scriptTag.dataset.announcement;
const announcementBackground = scriptTag.dataset.announcementBackground;
return {announcement, announcement_background: announcementBackground};
}
return null;
}
function setup() {
addRootDiv();
}
function init() {
const {apiUrl, previewData} = getSiteData();
setup();
ReactDOM.render(
<React.StrictMode>
<App
apiUrl={apiUrl}
previewData={previewData}
/>
</React.StrictMode>,
document.getElementById(ROOT_DIV_ID)
);
}
init();