react-misc v0.2.0
react-misc
Miscellaneous React components which I often use in my applications
CenterLayout
Summary
Center content inside it's parent container using flexbox
Usage
import { CenterLayout } from 'react-misc';
<CenterLayout
{/* flex parent */}
direction="row|row-reverse|column|column-reverse"
stretch={boolean}
{/* flex child */}
minWidth={string|number}
maxWidth={string|number}
minHeight={string|number}
maxHeight={string|number}
grow={boolean|number}
shrink={boolean|number}
basis={string|number}
>
{children}
</CenterLayout>
Name | Default Value | Description |
---|---|---|
direction | 'row' | Flex direction |
stretch | false | If the item should be stretched over the cross axis |
minWidth, maxWidth, minHeight, maxHeight | 0, none, 0, none | Min / max dimensions of the centered element |
grow, shrink, basis | false, false, 'auto' | Flex grow, shrink and basis |
FlowLayout
Summary
Order content in a horizontal or vertical flow using flexbox
Usage
import { FlowLayout } from 'react-misc';
<FlowLayout
direction="row|row-reverse|column|column-reverse"
wrap={boolean|'wrap|nowrap|wrap-reverse'}
justifyContent="flex-start|flex-end|center|space-between|space-around|space-evenly"
alignContent="flex-start|flex-end|center|space-between|space-around|stretch"
stretch={boolean}
>
<FlowLayout.Item
grow={boolean|number}
shrink={boolean|number}
basis={string|number}
>
{children}
</FlowLayout.Item>
</CenterLayout>
\<FlowLayout>
Name | Default Value | Description |
---|---|---|
direction, wrap, justifyContent, alignContent | 'column', false, 'flex-start', 'center' | Flex direction, wrap, justifyContent and alignContent |
stretch | true | If the items should be stretched over the cross axis |
\<FlowLayout.Item>
Name | Default Value | Description |
---|---|---|
grow, shrink, basis | false, false, 'auto' | Flex grow, shrink and basis |
HttpError
Summary
Display basic error messages for a selection of HTTP status codes using React Intl
Usage
You need to wrap this component into an <IntlProvider>
.
You can use custom translations for your language. Please look at src/components/HttpError/messages.js for the supported message types.
import { HttpError } from 'react-misc';
<HttpError code={number} />
HttpError.humanize(data: number|object, fieldName: string)
\<HttpError>
Name | Default Value | Description |
---|---|---|
code | undefined | HTTP status code |
The message for code 500 is displayed by default if the provided one is invalid or unsupported.
HttpError.humanize
Name | Default Value | Description |
---|---|---|
data | undefined | HTTP status code or error object |
fieldName | 'code' | The field to get the code from if data is an object |
Returns null
if data
is falsy, an <HttpError>
otherwise.
ShadowCompound
Summary
Wraps the component inside a container which takes up the same space using react-measure. This is helpful for fixed elements which overlay the body (e.g. navigation bars).
Usage
Children are handled using react-resolve-element. Please look at The concept for information on the supported render methods.
import { Shadowcompound } from 'react-misc';
<ShadowCompound
horizontal={boolean}
vertical={boolean}
component={ReactComponent}
render={function}
>
{children}
</ShadowCompound>
<ShadowCompound.MeasurePuppet
horizontal={boolean}
vertical={boolean}
innerRef={MeasureRef}
/>
\<ShadowCompound>
Name | Default Value | Description |
---|---|---|
horizontal | false | If the compound should take over the width of the measure component |
vertical | false | If the compound should take over the height of the measure component |
component, render, children | Render methods |
The child get's a property measureRef
which should be supplied to the element which size should be measured.
\<ShadowCompound.MeasurePuppet>
Name | Default Value | Description |
---|---|---|
horizontal | false | If the puppet should fill the parent's width |
vertical | false | If the puppet should fill the parent's height |
innerRef | undefined | The measureRef supplied by <ShadowCompound> |
This component can be used if the component you want to measure doesn't support ref's. It fills up the parent and hence can be used to measure the component's size.
Example
This example shows how to use this component with a navigation bar built with a Semantic UI React menu.
class Bar extends React.PureComponent { // eslint-disable-line react/prefer-stateless-function
render() {
const {
measureRef,
...props
} = this.props;
return (
<Menu {...props}>
{/* Measure Puppet */}
<ShadowCompound.MeasurePuppet vertical innerRef={measureRef} />
<Menu.Item>Test</Menu.Item>
<Menu.Item>Authentic Menu Item</Menu.Item>
</Menu>
);
}
}
function Navigation(props) {
return (
<ShadowCompound vertical>
<Bar fixed="top" borderless stackable {...props} />
</ShadowCompound>
)
}
function Page() {
return (
<div>
<Navigation />
This is page content
</div>
)
}
When you render <Page>
you should see the navigation bar on top, and This is page content
message below. You can try to switch the browser resolution between mobile and desktop to see the height change in runtime.