import {
Component as ReactComponent,
type ComponentClass,
type HTMLProps,
type ReactNode,
type JSX,
} from 'react';
/**
Automatically binds your `Component` subclass methods to the instance.
@see https://github.com/sindresorhus/auto-bind#autobindreactself-options
@param self - Object with methods to bind.
@example
```
import {autoBind} from 'react-extras';
class Foo extends Component {
constructor(props) {
super(props);
autoBind.react(this);
}
// …
}
```
*/
/**
Conditionally join CSS class names together.
@param input - Accepts a combination of strings and objects. Only object keys with truthy values are included. Anything else is ignored.
@example
```
import {classNames} from 'react-extras';
classNames('unicorn', 'rainbow');
//=> 'unicorn rainbow'
classNames({awesome: true, foo: false}, 'unicorn', {rainbow: false});
//=> 'awesome unicorn'
classNames('unicorn', null, undefined, 0, 1, {foo: null});
//=> 'unicorn'
const buttonType = 'main';
classNames({[`button-${buttonType}`]: true});
//=> 'button-main'
```
@example
```
import {classNames} from 'react-extras';
const Button = props => {
const buttonClass = classNames(
'button',
{
[`button-${props.type}`]: props.type,
'button-block': props.block,
'button-small': props.small
}
);
console.log(buttonClass);
//=> 'button button-success button-small'
return ;
};
```
*/
/**
Returns a boolean of whether the given `Component` is a functional stateless component.
@see https://javascriptplayground.com/functional-stateless-components-react/
*/
export function isStatelessComponent(component: ComponentClass): boolean;
/**
Returns the display name of the given `Component`.
@see https://reactjs.org/docs/react-component.html#displayname
*/
export function getDisplayName(component: ComponentClass): string;
/**
A boolean of whether you're running in a context with a DOM.
Can be used to check if your component is running in the browser or if it's being server-rendered.
@see https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction
*/
export const canUseDOM: boolean;
type IfProps = {
/**
Condition to check. Children are only rendered if `true`.
*/
readonly condition: boolean;
/**
Children to render if `condition` is `true`.
*/
readonly children?: ReactNode;
/**
If you need the children to not be evaluated when `condition` is `false`, pass a function to the `render` prop that returns the children.
*/
readonly render?: () => ReactNode;
};
/**
React component that renders the children if the `condition` prop is `true`.
Beware that even though the children are not rendered when the `condition` is `false`, they're still evaluated.
If you need it to not be evaluated on `false`, you can pass a function to the `render` prop that returns the children:
@example
```
import {If} from 'react-extras';
(
{props.error}
)}/>
```
Or you could just use plain JavaScript:
@example
```
import {If} from 'react-extras';
{props.error && (
{props.error}
)}
```
*/
export class If extends ReactComponent {}
type ChooseOtherwiseProps = {
/**
Children to render in the default case.
*/
readonly children?: ReactNode;
/**
If you need the children to not be evaluated when a `` component has a true condition, pass a function to the `render` prop that returns the children.
*/
readonly render?: () => ReactNode;
};
export class ChooseOtherwise extends ReactComponent {}
/**
React component similar to a switch case. `` has 2 children components:
- `` that renders the children if the `condition` prop is `true`.
- `` that renders the children if has no `` with `true` prop `condition`.
Note that even when the children are not rendered, they're still evaluated.
@example
```
import {Choose} from 'react-extras';
{props.success}
{props.error}
😎
```
@example
```
{(() => {
if (props.success) {
return
{props.success}
;
}
if (props.error) {
return
{props.error}
;
}
return
😎
;
})()}
```
*/
export class Choose extends ReactComponent {
/**
Renders the children if the `condition` prop is `true`.
Use with `Choose` and `Choose.Otherwise`.
*/
static When: typeof If;
/**
Renders the children if there is no `` with `true` prop `condition`.
Use with `Choose` and `Choose.When`.
*/
static Otherwise: typeof ChooseOtherwise;
}
type ForProps = {
/**
Items to iterate over. `render` will be called once per item.
*/
readonly of: readonly T[];
/**
Returns the element to render corresponding to an `item`.
*/
readonly render?: (item: T, index: number) => ReactNode;
};
/**
React component that iterates over the `of` prop and renders the `render` prop.
@example
```
import {For} from 'react-extras';
}/>
```
Or you could just use plain JavaScript:
@example
```
{['🌈', '🦄', '😎'].map((item, index) =>
)}
```
*/
export class For extends ReactComponent> {}
type ImageProps = {
/**
URL of the image. Use instead of `src`.
*/
readonly url: string;
/**
Fallback URL to display if the image does not load.
Default: Hide the image if it fails to load.
*/
readonly fallbackUrl?: string;
} & HTMLProps;
/**
React component that improves the `` element.
It makes the image invisible if it fails to load instead of showing the default broken image icon. Optionally, specify a fallback image URL.
@example
```
import {Image} from 'react-extras';
```
It supports all the props that `` supports, but you use the prop `url` instead of `src`.
*/
export class Image extends ReactComponent {}
type ElementClassProps = {
/**
Classes to add to the root element.
Either a single class or multiple classes separated by space.
*/
readonly add?: string;
/**
Classes to remove from the root element.
Either a single class or multiple classes separated by space.
*/
readonly remove?: string;
};
/**
Renderless React component that can add and remove classes to the root `` element. It accepts an `add` prop for adding classes, and a `remove` prop for removing classes. Both accept either a single class or multiple classes separated by space.
@example
```
import {If, RootClass} from 'react-extras';
```
@example
```
import {RootClass} from 'react-extras';
```
*/
export class RootClass extends ReactComponent {}
/**
Same as `` but for ``.
Prefer `` though, because it's nicer to put global classes on `` as you can consistently prefix everything with the class:
@example
```css
.dark-mode body {
background: #000;
}
.dark-mode a {
…
}
```
With `` you need to do:
@example
```css
body.dark-mode {
background: #000;
}
.dark-mode a {
…
}
```
*/
export class BodyClass extends ReactComponent {}
/**
Inserts a separator between each element of the children.
@param children - The elements to intersperse with separators.
@param separator - The separator to insert between elements. Can be a ReactNode or a function that returns a ReactNode.
@example
```
import {intersperse} from 'react-extras';
const items = ['Apple', 'Orange', 'Banana'];
const list = intersperse(
items.map(item =>
{item}
),
', '
);
// => [
Apple
, ', ',
Orange
, ', ',
Banana
]
```
@example
```
import {intersperse} from 'react-extras';
const items = ['Apple', 'Orange', 'Banana'];
const list = intersperse(
items.map(item =>
{item}
),
(index, count) => index === count - 2 ? ' and ' : ', '
);
// => [
Apple
, ', ',
Orange
, ' and ',
Banana
]
```
*/
export function intersperse(
children: ReactNode,
separator?: ReactNode | ((index: number, count: number) => ReactNode)
): ReactNode[];
type JoinProps = {
/**
The separator to insert between elements.
Can be a ReactNode or a function that returns a ReactNode.
@default ', '
*/
readonly separator?: ReactNode | ((index: number, count: number) => ReactNode);
/**
The elements to join with separators.
*/
readonly children: ReactNode;
};
/**
React component that renders the children with a separator between each element.
@example
```
import {Join} from 'react-extras';
Apple
Orange
Banana
// =>
Apple
,
Orange
,
Banana
```
@example
```
import {Join} from 'react-extras';
HomeAboutContact
// => Home | About | Contact
```
@example
```
import {Join} from 'react-extras';
index === count - 2 ? ' and ' : ', '}>
AppleOrangeBanana
// => Apple, Orange and Banana
```
*/
export function Join(props: JoinProps): JSX.Element;
export {default as classNames} from '@sindresorhus/class-names';
export {default as autoBind} from 'auto-bind/react';