Plugins
The react-better-html library allows you to configure the plugins through the <BetterHtmlProvider> wrapper component.
Make sure to never change the plugins array while the app is running. This can cause unexpected behavior.
Setup
To start using a plugin, you need to pass it to the plugins array in the <BetterHtmlProvider> component.
import { createRoot } from "react-dom/client";
import { BetterHtmlProvider, BetterHtmlPlugin, reactRouterDomPlugin } from "react-better-html";
import App from "./App";
const plugins: BetterHtmlPlugin[] = [
reactRouterDomPlugin()
];
const root = document.getElementById("root");
createRoot(root).render(
<BetterHtmlProvider plugins={plugins}>
<App />
</BetterHtmlProvider>
);
Available plugins
React Router DOM
The reactRouterDomPlugin plugin allows you to use the react-router-dom library to navigate to the links in the HTML. It also unlocks new functionalities for a number of components like the <Modal> and others. No configuration in needed when initializing the plugin.
Alerts
The alertsPlugin plugin enables alerts in the DOM. Can be used with the useAlertControls hook. Configuration can be passed when initializing the plugin.
alertsPlugin({
position: "bottom",
align: "right",
defaultDuration: "auto",
maxWidth: 460,
withLoaderBar: true,
withCloseButton: true
});
position: The position of the alerts. Can betoporbottom. Defaults tobottom.align: The alignment of the alerts. Can beleft,centerorright. Defaults toright.defaultDuration: The default duration of the alerts. Can beautoor a number in milliseconds. Defaults toauto.maxWidth: The maximum width of the alerts. Defaults to460.withLoaderBar: Whether to show a loader bar in the alert box. When the alert is hovered with the mouse the loader pauses. Defaults totrue.withCloseButton: Whether to show a close button in the alert box. Defaults totrue.
LocalStorage
The localStoragePlugin plugin enables localStorage functionality. Can be accessed from generateLocalStorage function. Configuration can be passed when initializing the plugin.
localStoragePlugin({
encryption: {
enabled: true,
secretKey: "YOUR_SECRET_KEY",
iv: "YOUR_IV"
}
});
encryption: The encryption options for the localStorage. Can be enabled withenabled: true. When enabled it requiressecretKeyandivproperties. Defaults toenabled: false.
To generate a secretKey and iv you can use the following node commands:
- Generate secretKey
- Generate iv
node -e 'console.log(`SECRET_KEY:`, require(`crypto`).randomBytes(32).toString(`hex`))'
node -e 'console.log(`IV:`, require(`crypto`).randomBytes(16).toString(`hex`))'