Skip to content

Commit 3e23a3b

Browse files
committed
docs: improve README with correct links and better structure
- Fix demo link to point to /examples/ instead of /index.html - Add npm and build status badges - Add Table of Contents - Add prominent "Extracting Styles" section (common issue) - Add transformScale prop to documentation - Format compatibility as table - Update file links to full GitHub URLs - Add License section
1 parent ff9f81b commit 3e23a3b

File tree

1 file changed

+90
-47
lines changed

1 file changed

+90
-47
lines changed

README.md

Lines changed: 90 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,126 @@
1-
### React-Resizable
1+
# React-Resizable
22

3-
[View the Demo](https://react-grid-layout.github.io/react-resizable/index.html)
3+
[![npm version](https://img.shields.io/npm/v/react-resizable.svg)](https://www.npmjs.com/package/react-resizable)
4+
[![npm downloads](https://img.shields.io/npm/dm/react-resizable.svg)](https://www.npmjs.com/package/react-resizable)
5+
[![Build Status](https://github.com/react-grid-layout/react-resizable/actions/workflows/test.yml/badge.svg)](https://github.com/react-grid-layout/react-resizable/actions/workflows/test.yml)
6+
7+
[**View the Demo**](https://react-grid-layout.github.io/react-resizable/examples/)
48

59
A simple widget that can be resized via one or more handles.
610

711
You can either use the `<Resizable>` element directly, or use the much simpler `<ResizableBox>` element.
812

9-
See the example and associated code in [ExampleLayout](/examples/ExampleLayout.js) and
10-
[ResizableBox](/lib/ResizableBox.js) for more details.
13+
See the example and associated code in [ExampleLayout](https://github.com/react-grid-layout/react-resizable/blob/master/examples/ExampleLayout.js) and
14+
[ResizableBox](https://github.com/react-grid-layout/react-resizable/blob/master/lib/ResizableBox.js) for more details.
15+
16+
## Table of Contents
17+
18+
- [Installation](#installation)
19+
- [Compatibility](#compatibility)
20+
- [Usage](#usage)
21+
- [Resizable](#resizable)
22+
- [ResizableBox](#resizablebox)
23+
- [Props](#props)
24+
- [Extracting Styles](#extracting-styles)
25+
- [Custom Resize Handles](#resize-handle)
26+
27+
## Installation
28+
29+
```bash
30+
$ npm install --save react-resizable
31+
```
32+
33+
## Extracting Styles
34+
35+
You **must** include the associated styles in your application, otherwise the resize handles will not be visible and will not work properly.
1136

12-
Make sure you use the associated styles in [/css/styles.css](/css/styles.css), as without them, you will have
13-
problems with handle placement and visibility.
37+
```js
38+
// In your JS/TS entry point:
39+
import 'react-resizable/css/styles.css';
40+
```
1441

15-
You can pass options directly to the underlying `DraggableCore` instance by using the prop `draggableOpts`.
16-
See the [demo](/examples/TestLayout.js) for more on this.
42+
Or import it in your CSS:
1743

18-
### Installation
44+
```css
45+
@import 'react-resizable/css/styles.css';
46+
```
1947

20-
$ npm install --save react-resizable
48+
If you're using a bundler that doesn't support CSS imports, you can find the styles at `node_modules/react-resizable/css/styles.css` and include them manually.
2149

22-
### Compatibility
50+
## Compatibility
2351

24-
[React-Resizable 3.x](/CHANGELOG.md#3.0.0) is compatible with React `>= 16.3`.
25-
React-Resizable 2.x has been skipped.
26-
[React-Resizable 1.x](/CHANGELOG.md#1.11.1) is compatible with React `14-17`.
52+
| Version | React Version |
53+
|---------|---------------|
54+
| [3.x](https://github.com/react-grid-layout/react-resizable/blob/master/CHANGELOG.md#300-may-10-2021) | `>= 16.3` |
55+
| 2.x | Skipped |
56+
| [1.x](https://github.com/react-grid-layout/react-resizable/blob/master/CHANGELOG.md#1111-mar-5-2021) | `14 - 17` |
2757

28-
### Usage
58+
## Usage
2959

3060
This package has two major exports:
3161

32-
* [`<Resizable>`](/lib/Resizable.js): A raw component that does not have state. Use as a building block for larger components, by listening to its
33-
callbacks and setting its props.
34-
* [`<ResizableBox>`](/lib/ResizableBox.js): A simple `<div {...props} />` element that manages basic state. Convenient for simple use-cases.
62+
* [`<Resizable>`](https://github.com/react-grid-layout/react-resizable/blob/master/lib/Resizable.js): A raw component that does not have state. Use as a building block for larger components, by listening to its callbacks and setting its props.
63+
* [`<ResizableBox>`](https://github.com/react-grid-layout/react-resizable/blob/master/lib/ResizableBox.js): A simple `<div {...props} />` element that manages basic state. Convenient for simple use-cases.
3564

65+
### `<Resizable>`
3666

37-
#### `<Resizable>`
3867
```js
39-
const {Resizable} = require('react-resizable');
40-
41-
// ES6
4268
import { Resizable } from 'react-resizable';
69+
import 'react-resizable/css/styles.css';
4370

44-
// ...
4571
class Example extends React.Component {
4672
state = {
4773
width: 200,
4874
height: 200,
4975
};
5076

51-
// On top layout
5277
onResize = (event, {node, size, handle}) => {
5378
this.setState({width: size.width, height: size.height});
5479
};
5580

5681
render() {
5782
return (
58-
<Resizable height={this.state.height} width={this.state.width} onResize={this.onResize}>
59-
<div className="box" style={{width: this.state.width + 'px', height: this.state.height + 'px'}}>
83+
<Resizable
84+
height={this.state.height}
85+
width={this.state.width}
86+
onResize={this.onResize}
87+
>
88+
<div
89+
className="box"
90+
style={{width: this.state.width + 'px', height: this.state.height + 'px'}}
91+
>
6092
<span>Contents</span>
6193
</div>
6294
</Resizable>
6395
);
6496
}
6597
}
66-
6798
```
6899

100+
### `<ResizableBox>`
69101

70-
#### `<ResizableBox>`
71102
```js
72-
const {ResizableBox} = require('react-resizable');
73-
74-
// ES6
75103
import { ResizableBox } from 'react-resizable';
104+
import 'react-resizable/css/styles.css';
76105

77106
class Example extends React.Component {
78107
render() {
79108
return (
80-
<ResizableBox width={200} height={200} draggableOpts={{grid: [25, 25]}}
81-
minConstraints={[100, 100]} maxConstraints={[300, 300]}>
109+
<ResizableBox
110+
width={200}
111+
height={200}
112+
draggableOpts={{grid: [25, 25]}}
113+
minConstraints={[100, 100]}
114+
maxConstraints={[300, 300]}
115+
>
82116
<span>Contents</span>
83117
</ResizableBox>
84118
);
85119
}
86120
}
87121
```
88122

89-
### Props
123+
## Props
90124

91125
These props apply to both `<Resizable>` and `<ResizableBox>`. Unknown props that are not in the list below will be passed to the child component.
92126

@@ -96,14 +130,15 @@ type ResizeCallbackData = {
96130
size: {width: number, height: number},
97131
handle: ResizeHandleAxis
98132
};
133+
99134
type ResizeHandleAxis = 's' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne';
100135

101-
type ResizableProps =
102-
{
136+
type ResizableProps = {
103137
children: React.Element<any>,
104138
width: number,
105139
height: number,
106-
// Either a ReactElement to be used as handle, or a function returning an element that is fed the handle's location as its first argument.
140+
// Either a ReactElement to be used as handle, or a function
141+
// returning an element that is fed the handle's location as its first argument.
107142
handle: ReactElement<any> | (resizeHandle: ResizeHandleAxis, ref: ReactRef<HTMLElement>) => ReactElement<any>,
108143
// If you change this, be sure to update your css
109144
handleSize: [number, number] = [10, 10],
@@ -115,7 +150,9 @@ type ResizableProps =
115150
onResizeStart?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
116151
onResize?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
117152
draggableOpts?: ?Object,
118-
resizeHandles?: ?Array<ResizeHandleAxis> = ['se']
153+
resizeHandles?: ?Array<ResizeHandleAxis> = ['se'],
154+
// If `transform: scale(n)` is set on the parent, this should be set to `n`.
155+
transformScale?: number = 1
119156
};
120157
```
121158
@@ -129,27 +166,29 @@ The following props can also be used on `<ResizableBox>`:
129166
130167
If a `width` or `height` is passed to `<ResizableBox>`'s `style` prop, it will be ignored as it is required for internal function.
131168
132-
#### Resize Handle
169+
You can pass options directly to the underlying `DraggableCore` instance by using the prop `draggableOpts`. See the [demo](https://react-grid-layout.github.io/react-resizable/examples/) for more on this.
170+
171+
## Resize Handle
133172
134-
If you override the resize handle, we expect that any `ref` passed to your new handle with represent the underlying DOM element.
173+
If you override the resize handle, we expect that any `ref` passed to your new handle will represent the underlying DOM element.
135174
136175
This is required, as `react-resizable` must be able to access the underlying DOM node to attach handlers and measure position deltas.
137176
138177
There are a few ways to do this:
139178
140-
##### Native DOM Element
179+
### Native DOM Element
141180
142181
This requires no special treatment.
143182
144183
```js
145184
<Resizable handle={<div className="foo" />} />
146185
```
147186
148-
##### Custom React Component
187+
### Custom React Component
149188
150189
You must [forward the ref](https://reactjs.org/docs/forwarding-refs.html) and props to the underlying DOM element.
151190
152-
###### Class Components
191+
#### Class Components
153192
154193
```js
155194
class MyHandleComponent extends React.Component {
@@ -163,7 +202,7 @@ const MyHandle = React.forwardRef((props, ref) => <MyHandleComponent innerRef={r
163202
<Resizable handle={<MyHandle />} />
164203
```
165204
166-
###### Functional Components
205+
#### Functional Components
167206
168207
```js
169208
const MyHandle = React.forwardRef((props, ref) => {
@@ -174,7 +213,7 @@ const MyHandle = React.forwardRef((props, ref) => {
174213
<Resizable handle={<MyHandle />} />
175214
```
176215
177-
##### Custom Function
216+
### Custom Function
178217
179218
You can define a function as a handle, which will simply receive an axis (see above `ResizeHandleAxis` type) and ref. This may be more clear to read, depending on your coding style.
180219
@@ -183,5 +222,9 @@ const MyHandle = (props) => {
183222
return <div ref={props.innerRef} className="foo" {...props} />;
184223
};
185224

186-
<Resizable handle={(handleAxis, ref) => <MyHandle innerRef={ref} className={`foo handle-${handleAxis}`} {...props} />} />
187-
```
225+
<Resizable handle={(handleAxis, ref) => <MyHandle innerRef={ref} className={`foo handle-${handleAxis}`} />} />
226+
```
227+
228+
## License
229+
230+
MIT

0 commit comments

Comments
 (0)