` 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 (
(isOpen ? setIsOpen.setTrue() : setIsOpen.setFalse())}
>
This foldable's state is controlled by the button above.
);
}
```
## Ref API
The foldable can be controlled using a ref with the following methods:
```typescript
type FoldableRef = {
isOpen: boolean;
open: () => void;
close: () => void;
toggle: () => void;
};
```
- **`open()`** - opens the foldable
- **`close()`** - closes the foldable
- **`isOpen`** - a boolean indicating whether the foldable is currently opened or not
- **`toggle()`** - toggles the foldable's open/closed state
## Custom Header
You can fully customize the header using the `renderHeader` prop:
```jsx
import { Foldable, Div, Text, Icon, useTheme } from "react-better-html";
function App() {
const theme = useTheme();
return (
(
Custom Header Design
)}
>
This foldable has a completely custom header.
);
}
```
## 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.
### Foldable.box
This component renders a foldable with predefined styles like the [`
`](https://krissvv.github.io/react-better-html/docs/components/div.md#divbox) component.
```jsx
Content goes here
```
---
# Table
> A customizable table component with support for various column types and styling options.
Source: https://krissvv.github.io/react-better-html/docs/components/table
The `` component is a versatile React component that provides a customizable table with support for different column types, styling options, and interactive features.
## Basic Usage
The Table component requires two main props: `columns` and `data`. The `columns` prop defines the structure of the table, while the `data` prop provides the content.
**Basic:**
```jsx
import { Table } from "react-better-html";
function App() {
return (
);
}
```
**Striped:**
```jsx
import { Table } from "react-better-html";
function App() {
return (
);
}
```
## Common Props
The Table component accepts the following props:
- **`columns`** - array of column definitions
- **`data`** - array of data objects
- **`isStriped`** - whether to display alternating row colors
- **`isLoading`** - renders a loader instead of the rows
- **`withStickyHeader`** - whether to make the header stick to the top when scrolling
- **`noDataItemsMessage`** - message to display when there's no data (defaults to `No data available`)
- **`getRowStyle`** - function that returns style props for a specific row
- **`borderRadius`** - the border radius of the table in pixels (defaults to `theme.styles.borderRadius * 2`)
- **`borderTopLeftRadius`**, **`borderTopRightRadius`**, **`borderBottomLeftRadius`**, **`borderBottomRightRadius`** - the border radius of a single corner in pixels (defaults to `borderRadius`)
- **`containsOverflowComponents`** - set it when cells contain elements that need to overflow the table (dropdowns, tooltips, ...)
- **`onClickRow`** - callback function when a row is clicked
- **`onClickAllCheckboxes`** - callback function when the header checkbox is clicked. Receives the new `checked` state
- **`pageSize`** - the number of items to display per page
- **`pageCount`** - the total number of pages. Used for when the rest of the items in the table are not present.
- **`onChangePage`** - callback function when the page changes. Used together with `pageSize` and `pageCount`.
- **`onChangeFilter`** - callback function when the filter changes
- **`onChangeFilterDataValue`** - callback function when the filter are applied and the data array is filtered.
- **`defaultFilterData`** - the filters applied on mount. The keys are the indexes of the columns.
**Loading:**
```jsx
import { Table } from "react-better-html";
function App() {
return (
);
}
```
**Row Style:**
```jsx
import { Table } from "react-better-html";
function App() {
return (
({
opacity: item.status === "Blocked" ? 0.5 : undefined,
})}
/>
);
}
```
**Pagination:**
```jsx
import { Table } from "react-better-html";
function App() {
return (
console.log("Current page:", page)}
/>
);
}
```
For server side pagination pass `pageCount` and load the data for the page in `onChangePage`.
## Column Props & Types
All column types support the following properties:
- **`hidden`** - hides the column
- **`label`** - the column header text
- **`renderLabel`** - function to render a custom header. Receives the `label`
- **`width`** - the width of the column (in pixels)
- **`minWidth`** - the minimum width of the column (in pixels)
- **`maxWidth`** - the maximum width of the column (in pixels)
- **`align`** - the text alignment within the column
- **`clickStopPropagation`** - whether to stop the click event from propagating to the parent element
- **`filter`** - the filter type for the column. Learn more in the [Filters](https://krissvv.github.io/react-better-html/docs/components/table.md#column-filters) section.
Column type-specific properties:
### Text Column
The text column type displays a text from the specified value in your data objects.
```jsx
import { Table } from "react-better-html";
function App() {
return (
`${item.age} years old`,
getTextProps: {
color: "#ff0000"
}
}
]}
data={[
{ name: "John Doe", age: 28 },
{ name: "Jane Smith", age: 32 }
]}
/>
);
}
```
Text columns support the following props:
- **`type`** - must be set to `"text"`
- **`keyName`** - the key in your data object to display
- **`format`** - optional function that receives the item and its index and returns the text to display
- **`getTextProps`** - optional object or function to get props for the rendered text component inside the table cell. All props from the [``](https://krissvv.github.io/react-better-html/docs/components/text.md) component are supported
### Element Column
The element column allows you to render custom React elements for each cell.
```jsx
import { Table, Text } from "react-better-html";
function App() {
return (
(
{item.status}
)
}
]}
data={[
{ name: "John Doe", status: "Active" },
{ name: "Jane Smith", status: "Inactive" }
]}
/>
);
}
```
Element columns support the following props:
- **`type`** - must be set to `"element"`
- **`render`** - a function that returns a React element, receives the item and index as parameters
### Image Column
The image column displays images from URLs stored in your data.
```jsx
import { Table } from "react-better-html";
function App() {
return (
({
src: item.avatarUrl,
width: 50,
height: 50,
borderRadius: 999
})
},
{
type: "text",
label: "Name",
keyName: "name"
}
]}
data={[
{ name: "John Doe", avatarUrl: "https://example.com/john.jpg" },
{ name: "Jane Smith", avatarUrl: "https://example.com/jane.jpg" }
]}
/>
);
}
```
Image columns support the following props:
- **`type`** - must be set to `"image"`
- **`getImageProps`** - optional object or function to get props for the rendered image component inside the table cell. All props from the [``](https://krissvv.github.io/react-better-html/docs/components/image.md) component are supported
### Checkbox Column
The checkbox column displays checkboxes that can be toggled. The checked state is kept inside the table and can be read/changed with the `onChange` from `getToggleInputProps` and the [`setCheckedItems`](#ref-api) ref method.
```jsx
import { Table } from "react-better-html";
function App() {
return (
console.log(item.name, checked)
}
},
{
type: "text",
label: "Name",
keyName: "name"
}
]}
data={[{ name: "John Doe" }, { name: "Jane Smith" }]}
onClickAllCheckboxes={(checked) => console.log("All checked:", checked)}
/>
);
}
```
Checkbox columns support the following props:
- **`type`** - must be set to `"checkbox"`
- **`getToggleInputProps`** - optional object or function to get props for the rendered toggle input component inside the table cell. All props from the [``](https://krissvv.github.io/react-better-html/docs/components/form/toggleInput.md) component are supported
> **Tip:**
>
> The table prop `onClickAllCheckboxes` can be used to render a checkbox in the header that will toggle all checkboxes at ones.
### Expand Column
The expand column displays an arrow that can be toggled to show/hide additional content.
```jsx
import { Table, Text } from "react-better-html";
function App() {
return (
{item.isSelected ? "Selected" : "Not selected"}
},
{
type: "text",
label: "Name",
keyName: "name"
}
]}
data={[
{ name: "John Doe", isSelected: true },
{ name: "Jane Smith", isSelected: false }
]}
/>
);
}
```
Expand columns support the following props:
- **`type`** - must be set to `"expand"`
- **`onlyOneExpanded`** - whether to allow only one row to be expanded at a time
- **`render`** - function to render the content inside the expanded row
- **`onExpand`** - optional callback function to be called when a row is expanded
- **`onCollapse`** - optional callback function to be called when a row is collapsed
> **Warning:**
>
> The table prop `isInsideTableExpandRow` must be set when rendering a `` component inside a `Expand` column. It is set to the inner `` component.
## Column Filters
### Number filter
```jsx
import { Table } from "react-better-html";
function App() {
return (
item.age
}
]}
data={[
{ name: "John Doe", age: 23 },
{ name: "Jane Smith", age: 28 }
]}
/>
);
}
```
Number filter support the following props:
- **`filter`** - must be set to `"number"`
- **`getValue`** - function to get the value for the filter check
### Date and Date-Time filter
```jsx
import { Table } from "react-better-html";
function App() {
return (
new Date(item.dateBorn)
}
]}
data={[
{ name: "John Doe", dateBorn: "2002-01-18" },
{ name: "Jane Smith", dateBorn: "1997-07-02" }
]}
/>
);
}
```
Date and Date-Time filters support the following props:
- **`filter`** - must be set to either `"date"` or `"date-time"`
- **`getValue`** - function to get the value for the filter check
- **`presets`** - optional array of presets to be displayed in the filter modal UI. Available presets: `today`, `yesterday`, `tomorrow`, `thisWeek`, `thisMonth`, `thisYear`, `lastWeek`, `lastMonth`, `lastYear`, `nextWeek`, `nextMonth`, `nextYear`
### List filter
```jsx
import { Table } from "react-better-html";
function App() {
return (
);
}
```
List filter support the following props:
- **`filter`** - must be set to `"list"`
- **`withTotalNumber`** - whether to show the total number of items in the filter modal
- **`withSearch`** - whether to render a search input in the filter modal
- **`list`** - the array of items to be displayed in the list as possible filter values
- **`getItemValue`** - optional function to get the value of the item to compare against the list values (defaults to the value of `keyName`)
## Memoized Filters
When `memoizeFilters` is passed together with a `name`, the applied filters are saved and restored the next time the table is rendered. The `memoizeFiltersLifespan` prop sets how long (in milliseconds) the filters are kept (defaults to 2 hours). The time resets every time the filters change.
```jsx
```
## Ref API
The table can be controlled using a ref:
```typescript
type TableRef = {
currentPage: number;
setCurrentPage: React.Dispatch>;
pagesCount: number;
setCheckedItems: React.Dispatch>;
};
```
- **`currentPage`** - the current page
- **`setCurrentPage`** - changes the current page
- **`pagesCount`** - the total number of pages
- **`setCheckedItems`** - changes the checked state of the rows (used with the [Checkbox Column](#checkbox-column))
---
# Tabs
> A tabs component with different styles and URL sync.
Source: https://krissvv.github.io/react-better-html/docs/components/tabs
The `` component renders a list of tabs with an animated indicator and shows the content of the selected tab with the `` subcomponent.
## Basic Usage
The component requires a `tabs` prop. Every tab is rendered with its `label` (or `id` if there is no label). The content of each tab is wrapped in `` with the matching `tabId`.
**Basic:**
```jsx
import { Tabs, Text } from "react-better-html";
function App() {
return (
Profile settings
Security settings
Billing settings
);
}
```
**Border Radius Top:**
```jsx
import { Tabs } from "react-better-html";
function App() {
return (
);
}
```
**Box:**
```jsx
import { Tabs } from "react-better-html";
function App() {
return (
);
}
```
**Bubble:**
```jsx
import { Tabs } from "react-better-html";
function App() {
return (
);
}
```
The selected tab background slides between the tabs.
## Common Props
- **`tabs`** - array of tabs (see [Tab object](#tab-object))
- **`style`** - `default`, `borderRadiusTop`, `box` or `bubble`
- **`direction`** - `horizontal` or `vertical` (defaults to `horizontal`). See [Vertical tabs](#vertical-tabs)
- **`verticalLinePosition`** - the side of the indicator line when the direction is `vertical` - `left` or `right` (defaults to `left`)
- **`iconPosition`** - the icon position for all tabs - `left` or `right` (defaults to `left`). A tab's own `iconPosition` takes priority
- **`width`** - the width of the whole component (defaults to `100%`)
- **`height`** - the height of the whole component (defaults to `auto`). When set, the tabs and the content scroll inside of it
- **`tabWidth`** - the width for all tabs (defaults to `fit-content`). A tab's own `width` takes priority
- **`alignTabs`** - the alignment of the tabs and the `rightElement` (defaults to `flex-end` for horizontal and `stretch` for vertical direction)
- **`rightElement`** - an element rendered on the right side of the tabs row (e.g. a button). With vertical direction it is rendered under the tabs
- **`accentColor`** - the color of the indicator and the selected tab (defaults to `primary`)
- **`name`** - the name of the tab group. Required when there is more than one `` on the page
- **`gap`** - the space between the tabs
- **`contentGap`** - the space between the tabs and the content (defaults to `theme.styles.space`)
- **`borderRadius`** - the border radius of the tabs
- **`noBottomLine`** - hides the indicator line
- **`onChange`** - called with the `id` of the newly selected tab (also called on mount)
- All margin props (`margin`, `marginBottom`, ...)
### Tab object
- **`id`** - the unique identifier of the tab
- **`label`** - the text of the tab (defaults to the `id`)
- **`hidden`** - hides the tab
- **`icon`** - the name of the icon of type [IconName](https://krissvv.github.io/react-better-html/docs/types/icon-config.md#iconname)
- **`iconPosition`** - whether to put the icon on the `left` or on the `right` side of the label (defaults to the `iconPosition` prop of ``)
- **`iconColor`** - the color of the icon (defaults to the color of the label)
- **`iconSize`** - the size of the icon (defaults to `16`)
- **`width`** - the width of the tab (defaults to the `tabWidth` prop of ``)
### Icons
Every tab can have an icon next to its label. The icon follows the color of the label, so it changes together with the selected state.
```jsx
import { Tabs } from "react-better-html";
function App() {
return (
);
}
```
The `iconPosition` prop of `` is applied to all tabs. A tab with its own `iconPosition` always uses it instead.
### Tab width
By default every tab is as wide as its content. The width can be set for all tabs with the `tabWidth` prop or for a single tab with the `width` field of the tab. A tab's own `width` always takes priority.
**Full Width:**
```jsx
import { Tabs } from "react-better-html";
function App() {
return (
);
}
```
The tabs share the available horizontal space equally - 4 tabs with `"100%"` width are 25% wide each.
**Mixed:**
```jsx
import { Tabs } from "react-better-html";
function App() {
return (
);
}
```
The `settings` tab is always `120px` wide and the other tabs take the rest of the space.
A tab with `"100%"` width never gets smaller than its content. When the container is smaller than the tabs, the tabs row becomes horizontally scrollable.
### Vertical tabs
With `direction="vertical"` the tabs are rendered in a column and the content is shown on the right side of them. All styles are supported. The indicator line is on the left side of the tabs by default and can be moved to the right side with `verticalLinePosition="right"`. The `borderRadiusTop` style rounds the corners on the opposite side of the line.
```jsx
import { Tabs, Text } from "react-better-html";
function App() {
return (
Profile settings
Security settings
Billing settings
);
}
```
By default the component is as high as its content. Set the `height` prop to limit it - the tabs column and the content then scroll inside of it.
```jsx
...
```
All tabs have the same width - the width of the widest tab. Use `tabWidth` (e.g. `tabWidth={200}`) to set a fixed width of the tabs column. The labels are aligned to the left.
### Hidden tabs
A tab with `hidden: true` is not rendered. If the selected tab gets hidden, the first visible tab is selected.
```jsx
import { Tabs } from "react-better-html";
function App({ isAdmin }) {
return (
);
}
```
### Multiple tab groups
`` is connected to its `` by the group `name`. It does not need to be a child of the `` component - it can be anywhere in the tree.
```jsx
import { Div, Tabs, Text } from "react-better-html";
function App() {
return (
This week
This month
);
}
```
## Tabs.content
- **`tabId`** - the `id` of the tab this content belongs to
- **`tabsGroupName`** - the `name` of the `` group
- **`tabWithDot`** - renders a small dot on the tab (e.g. for unread items or errors inside the tab)
- **`isInitialTab`** - renders the content before the `` group has registered (avoids a flash of empty content)
```jsx
...
```
## Ref API
The tabs can be controlled using a ref:
```typescript
type TabsRef = {
selectedTab: string;
selectTab: (tabId: string) => void;
};
```
- **`selectedTab`** - the `id` of the selected tab
- **`selectTab()`** - selects a tab by its `id`
## Used With Plugins
With the [`react-router-dom`](https://krissvv.github.io/react-better-html/docs/plugins.md#react-router-dom) plugin the selected tab is saved in the URL as a query parameter (`?tab=security` or `?{name}=security` when `name` is passed) and restored when the page is opened. Pass `keepUrlHistory={false}` to replace the URL instead of adding a new history entry on every tab change.
---
# Tooltip
> A tooltip and popover component that can also be used as a context menu.
Source: https://krissvv.github.io/react-better-html/docs/components/tooltip
The `` component renders floating content next to its children. It can be opened on hover or on click, stays inside the screen and can be used as a simple tooltip, a popover or a menu.
## Basic Usage
The component requires a `content` prop. The `children` are the element the tooltip is attached to.
**Basic:**
```jsx
import { Tooltip, Button } from "react-better-html";
function App() {
return (
);
}
```
**With Arrow:**
```jsx
import { Tooltip, Icon } from "react-better-html";
function App() {
return (
);
}
```
**Click Trigger:**
```jsx
import { Tooltip, Button, Div, Text } from "react-better-html";
function App() {
return (
Share
Copy the link to share this page.
}
contentWidth={240}
>
);
}
```
Opens on click and closes when clicking outside.
**Menu:**
```jsx
import { Tooltip, Button } from "react-better-html";
function App() {
return (
{}} />
{}} />
>
}
contentMinWidth={220}
>
);
}
```
`asContextMenu` removes the inline padding of the content so the items fill the whole width.
## Common Props
- **`content`** - the content of the tooltip. Can be a string or any element
- **`position`** - `top`, `bottom` (default), `left` or `right`. The tooltip is shifted to stay inside the screen
- **`align`** - the alignment along the side - `left`, `center` (default) or `right` for `top`/`bottom` positions and `top`, `center` or `bottom` for `left`/`right` positions
- **`trigger`** - `hover` (default) or `click`
- **`withArrow`** - renders an arrow pointing to the children
- **`isSmall`** - smaller padding of the content
- **`backgroundColor`** - the background color of the content
- **`asContextMenu`** - styles the content as a menu
- **`disabled`** - the tooltip is not opened
- **`isTabAccessed`** - the children can be focused with the `Tab` key
- **`contentWidth`** / **`contentMinWidth`** - the size of the content
- **`contentPaddingBlock`** / **`contentPaddingInline`** - the padding of the content
- **`contentPointerEvents`** - the `pointer-events` of the content (pass `none` for tooltips that should not be hovered)
- **`childrenWrapperWidth`** / **`childrenWrapperHeight`** - the size of the element that wraps the children (width defaults to `fit-content`)
- **`onOpen`** / **`onClose`** - callbacks when the tooltip is opened or closed
## Ref API
The tooltip can be controlled using a ref:
```typescript
type TooltipRef = {
isOpen: boolean;
open: () => void;
close: () => void;
};
```
```jsx
import { useRef } from "react";
import { Tooltip, TooltipRef, Button } from "react-better-html";
function App() {
const tooltipRef = useRef(null);
return (
{
duplicateItem();
tooltipRef.current?.close();
}}
/>
}
ref={tooltipRef}
>
);
}
```
## Subcomponents
The subcomponents are building blocks for the `content` of the tooltip.
### Tooltip.item
A clickable row with an optional icon and description.
- **`text`** / **`textColor`** - the text of the item
- **`description`** - a smaller text underneath
- **`icon`** / **`iconColor`** - an icon before the text
- **`isActive`** - renders the text in bold
- **`disabled`** - the item can not be clicked
- **`onClick`** / **`onClickWithValue`** - click handlers. `onClickWithValue` is used together with the `value` prop
### Tooltip.sectionTitle
A small uppercase title for a group of items. Accepts all [``](https://krissvv.github.io/react-better-html/docs/components/text.md) props.
### Tooltip.divider
A horizontal line between items. Accepts all [``](https://krissvv.github.io/react-better-html/docs/components/Other/divider.md) props.
---
# Form
> A layout component for forms, integrating with the useForm hook
Source: https://krissvv.github.io/react-better-html/docs/components/form
The `
);
}
```
**With useForm:**
```jsx
import { Form, InputField, useForm } from "react-better-html";
function LoginForm() {
const form = useForm({
defaultValues: {
username: "",
password: "",
},
requiredFields: ["username", "password"],
onSubmit: async (values) => {
await login(values);
},
});
return (
);
}
```
The submit button is disabled while a required field is empty and is in loading state while the `onSubmit` promise is pending.
**With Cancel:**
```jsx
import { Form, InputField } from "react-better-html";
function EditForm() {
return (
);
}
```
## Common Props
- **`form`** - the object returned by the [`useForm`](https://krissvv.github.io/react-better-html/docs/hooks/use-form.md) hook.
- **`name`** / **`id`** - passed to the `