# react-better-html > A React 19 component library that stays as close to plain HTML as possible. Every component accepts all of its HTML element's attributes plus every CSS property as a prop (with a `Hover` variant), and common variations are exposed as dot subcomponents like `Div.row` or `Button.secondary`. It is built on styled-components and themed through a single ``. The full documentation in one file: https://krissvv.github.io/react-better-html/llms-full.txt Every page below is also available as clean markdown by appending `.md` to its docs URL. Rules for writing code with react-better-html: - Install with `npm install react-better-html`. Everything (components, hooks, functions and types) is imported from `"react-better-html"`. - Wrap the app once in ``. Hooks, controls and most components throw or render incorrectly without it. Plugins are passed with `` and must never change at runtime. - Use `
` and `` instead of raw `
`, `

`, `

`, etc. Change the rendered tag with `as` (``, `
`). Use `Div.row`, `Div.column` and `Div.grid` instead of writing `display: flex` by hand. - Style with props, not `style` or CSS files: `
`. Numbers are pixels. Every CSS prop has a hover version with the `Hover` suffix: `backgroundColorHover`, `colorHover`, `filterHover`. - Take spacing and colors from the theme instead of hardcoding: `const theme = useTheme();` then `theme.styles.space` (16), `theme.styles.gap` (8), `theme.styles.borderRadius` (10), `theme.colors.primary`, `theme.colors.textPrimary`, `theme.colors.textSecondary`, `theme.colors.backgroundContent`, `theme.colors.border`. Colors follow the current light or dark theme automatically. - Subcomponents are presets: `Button.secondary`, `Button.destructive`, `Button.text`, `Button.icon`, `Button.upload`, `Text.unknown`, `Text.oneLine`, `Div.box`, `InputField.email`, `InputField.password`, `InputField.search`, `InputField.date`, `Modal.confirmation`, `Modal.destructive`, `Tabs.content`, `Tooltip.item` and more. `ToggleInput` and `Divider` can only be used as subcomponents (`ToggleInput.checkbox`, `ToggleInput.radiobutton`, `ToggleInput.switch`, `Divider.horizontal`, `Divider.vertical`). - Prefer the value-based callbacks: `onChangeValue={(value) => ...}` on `InputField`, `onChange={(value) => ...}` on `Dropdown`, `onChange={(checked, value) => ...}` on `ToggleInput`, and `value` + `onClickWithValue={(value) => ...}` on `Div`, `Button` and `Chip`. - Imperative components are controlled with a ref: `const modalRef = useRef(null); modalRef.current?.open();` (also `TooltipRef`, `TabsRef`, `FoldableRef`, `TableRef`). - Build forms with `useForm({ defaultValues, requiredFields, validate, onSubmit })` and `
`, then spread `form.getInputFieldProps("field")`, `form.getDropdownFieldProps(...)`, `form.getCheckboxProps(...)`, `form.getRadioButtonProps(field, value)`, `form.getSwitchProps(...)` or `form.getTextAreaProps(...)` onto the fields. Use `FormRow` to put two fields on one line. - Icon names (``, `icon` props) must exist in `config.icons` and asset names (``, `image` props) in `config.assets`. Built-in icons: `XMark`, `uploadCloud`, `trash`, `chevronDown`, `chevronLeft`, `chevronRight`, `doubleChevronLeft`, `doubleChevronRight`, `eye`, `eyeDashed`, `magnifyingGlass`, `check`, `infoI`, `warningTriangle`, `filter`. The `logo` asset is an empty placeholder meant to be overridden. Use `src` for image URLs. - Global loading states use named loaders: `useLoader("name")` to read, `useLoaderControls()` or `loaderControls` to start and stop, `loaderName="name"` on `Button`. - Alerts need `alertsPlugin()` and are created with `useAlertControls().createAlert({ type, title, message })` or `alertControls.createAlert(...)`. `Modal` `name` and `Tabs` URL sync need `reactRouterDomPlugin()`. `generateLocalStorage` needs `localStoragePlugin()`. - Make layouts responsive with `useMediaQuery()` (`mediaQuery.size600`, `size900`, `size1000`, ... are `true` at or below that width) combined with `invertFlexDirection` on `Div.row` / `Div.column`. - Default component styles can be set app-wide in `config.components` (for example `components.button.style.default`). Set `components.button.tagReplacement.linkComponent` to the router `Link` so `Button href` and `SideMenu` items navigate client-side. --- # Introduction Source: https://krissvv.github.io/react-better-html/docs/introduction 🔴 The library is not yet ready for use - development is actively in progress 🔴 Welcome to the documentation for the `react-better-html` library! This library provides a set of utilities and components to enhance your React applications with better HTML handling and manipulation. Whether you are building a complex web application or a simple static site, `react-better-html` aims to simplify your development process by offering reusable and efficient solutions with as little lines of code as possible. ## Key Features - ♻️ **Reusable Components** - A collection of well-designed React components that can be easily integrated into your projects. - 🛠️ **Utility Functions** - Handy utility functions to manipulate and handle HTML content effectively. - 📘 **TypeScript Support** - Fully typed with TypeScript to provide a better development experience and catch errors early. - 🔌 **Easy Integration** - Designed to be easily integrated with existing React projects with minimal configuration. ## Getting Started To get started with `react-better-html`, check out the [installation guide](https://krissvv.github.io/react-better-html/docs/getting-started/instalation.md) and explore the docs for detailed information on how to use the library. We hope you find `react-better-html` useful and it helps you build better React applications. Happy coding! --- # Installation > How to install react-better-html, and start using it. Source: https://krissvv.github.io/react-better-html/docs/getting-started/instalation Welcome to the installation guide for `react-better-html`. This guide will help you get started with installing and using the library in your React projects. ## Requirements - [React](https://react.dev) version 19.0 or above. The library uses [styled-components](https://styled-components.com) under the hood to create all of the components in the library. `styled-components` is a peer dependency and will be installed automatically when downloading the `react-better-html`. ## Installation To install `react-better-html` run the following command in your project directory: **npm:** ```bash npm install react-better-html ``` ## Quick start Wrap your app with the [``](https://krissvv.github.io/react-better-html/docs/getting-started/configuration.md) and start using the components. ```jsx import { createRoot } from "react-dom/client"; import { BetterHtmlProvider } from "react-better-html"; import App from "./App"; createRoot(document.getElementById("root")).render( ); ``` ```jsx import { PageHolder, PageHeader, Div, Button } from "react-better-html"; function App() { return (
); } ``` **Customizable:** ```jsx import { useRef } from "react"; import { Div, Text, Button, Modal, ModalRef } from "react-better-html"; function App() { const myModalRef = useRef(null); return (
); } ``` **Outside Components:** ```jsx import { useRef } from "react"; import { Div, Text, Button, Modal, ModalRef } from "react-better-html"; function App() { const myModalRef = useRef(null); return (
); } ``` Elements rendered outside of the modal box, above and bellow it. ## Common Props - **`maxWidth`** - the maximum width of the modal. Defaults to 30% smaller than `app.contentMaxWidth` from the [``](https://krissvv.github.io/react-better-html/docs/getting-started/configuration.md#app-configuration) config. `660` looks good for small modals. - **`icon`** - an icon rendered before the title. - **`title`** - this prop enables the header of the modal and renders a title for the modal. - **`titleColor`** - the color of the title text. - **`description`** - this prop requires `title` prop and renders a description text underneath it. - **`descriptionColor`** - the color of the description text. - **`headerTextAlign`** - aligns the header content to the `left` (default) or to the `center`. - **`headerBackgroundColor`** - the background color of the modal's header. - **`backgroundColor`** - the background color of the modal. - **`withoutCloseButton`** - hides the `X` button. - **`closeButtonLight`** - renders the `X` button in a light color (useful with a dark `headerBackgroundColor`). - **`defaultIsOpened`** - opens the modal on mount. - **`overflow`** - the overflow of the modal's body. - **`outsideComponentAbove`** / **`outsideComponentBellow`** - elements rendered outside of the modal box. - **`onOpen`** - a callback function that is called when the modal is opened. - **`onClose`** - a callback function that is called when the modal is closed. - **`children`** - the content of the modal. All padding props (`padding`, `paddingInline`, ...) are applied to the modal's body. > **Tip:** > > The modal closes when the `Escape` key is pressed. ## Ref API The modal can be controlled using a ref with the following methods: ```typescript type ModalRef = { isOpened: boolean; open: () => void; close: () => void; }; ``` - **`open()`** - opens the modal - **`close()`** - closes the modal - **`isOpened`** - a boolean indicating whether the modal is currently opened or not ## Subcomponents A number of components in the library have a _subcomponent_ feature witch is like a preset of the same component that is frequently used. ### Modal.confirmation This component is a confirmation modal with `Continue` and `Cancel` buttons. It closes when any of the buttons is clicked. ```jsx import { useRef } from "react"; import { Div, Button, Modal, ModalRef } from "react-better-html"; function App() { const confirmationModalRef = useRef(null); return (
); } ``` - **`message`** - the text inside the modal (has a default value) - **`continueButtonText`** - the text of the continue button (defaults to `Continue`) - **`continueButtonLoaderName`** - the loader name for the continue button - **`cancelButtonText`** - the text of the cancel button (defaults to `Cancel`) - **`onContinue`** - called when the continue button is clicked - **`onCancel`** - called when the cancel button is clicked ### Modal.destructive This component is a destructive modal with `Delete` and `Cancel` buttons. ```jsx import { useRef } from "react"; import { Div, Button, Modal, ModalRef } from "react-better-html"; function App() { const deleteModalRef = useRef(null); return (
); } ``` - **`message`** - the text inside the modal (has a default value) - **`deleteButtonText`** - the text of the delete button (defaults to `Delete`) - **`deleteButtonIconName`** - the icon of the delete button (defaults to `trash`) - **`deleteButtonLoaderName`** - the loader name for the delete button - **`cancelButtonText`** - the text of the cancel button (defaults to `Cancel`) - **`onDelete`** - called when the delete button is clicked - **`onCancel`** - called when the cancel button is clicked ## Used With Plugins The `` component can be used with the [`react-router-dom`](https://krissvv.github.io/react-better-html/docs/plugins.md#react-router-dom) plugin to set a query parameter when the modal is opened. This is especially useful when you want the path to changed when a modal is opened for analytics purposes. The only thing you need to do beside applying the plugin is to pass a `name` prop to the modal. ```jsx ... ``` While the modal is opened the URL contains the `?edit-user-modal=opened` query parameter. --- # Foldable > A collapsible component with customizable header and content Source: https://krissvv.github.io/react-better-html/docs/components/foldable The `` component provides a collapsible container with an animated transition between open and closed states. It features a customizable header with title, description, optional icon or image, and a rotating arrow indicator. ## Basic Usage The Foldable component can be used in both controlled and uncontrolled modes, and offers various customization options for the header and content. **Basic:** ```jsx import { Foldable, Text } from "react-better-html"; function App() { return ( This content will be shown or hidden when the header is clicked. The component animates smoothly between states. ); } ``` **With Description:** ```jsx import { Foldable, Text } from "react-better-html"; function App() { return ( This content will be shown or hidden when the header is clicked. The component animates smoothly between states. ); } ``` **With Icon:** ```jsx import { Foldable, Text } from "react-better-html"; function App() { return ( This content will be shown or hidden when the header is clicked. The component animates smoothly between states. ); } ``` **With Image:** ```jsx import { Foldable, Text } from "react-better-html"; function App() { return ( This content will be shown or hidden when the header is clicked. The component animates smoothly between states. ); } ``` ## Common Props The header accepts all the props of the [``](https://krissvv.github.io/react-better-html/docs/components/structure/page-header.md) component (`icon`, `image`, `title`, `description`, `rightElement` and more). All `
` style props are applied to the holder. - **`defaultOpen`** - whether the foldable is opened on mount (uncontrolled mode) - **`arrowPosition`** - render the arrow on the `left` or on the `right` (default) side of the header - **`withoutLine`** - removes the divider between the header and the content when opened - **`headerPaddingBlock`** / **`headerPaddingInline`** - the padding of the header ```jsx import { Foldable, Text } from "react-better-html"; function App() { return ( Delivery in 2-3 business days. ); } ``` ## Controlled Mode You can control the Foldable component's open/closed state externally: ```jsx import { Foldable, Button, Div, Text, useBooleanState } from "react-better-html"; function App() { const [isOpen, setIsOpen] = useBooleanState(); return (