UNPKG

4.18 kBMarkdownView Raw
1# React Tether
2
3![CI Status](https://github.com/danreeves/react-tether/actions/workflows/main.yml/badge.svg)
4[![Sauce Test Status](https://app.saucelabs.com/buildstatus/react-tether)](https://app.saucelabs.com/u/react-tether)
5
6> Cross-browser Testing Platform and Open Source <3 Provided by [Sauce Labs](https://saucelabs.com/).
7
8---
9
10React wrapper around [Tether](https://github.com/shipshapecode/tether), a positioning engine to make overlays, tooltips and dropdowns better
11
12![React Tether](images/tether-header.png)
13
14## Install
15
16`npm install react-tether`
17
18**As of version 2, a minimum of React 16.3 is required. If you need support for React < 16.3 please use the [1.x branch](https://github.com/danreeves/react-tether/tree/1.x).**
19
20## Example Usage
21
22```javascript
23import TetherComponent from "react-tether";
24
25class SimpleDemo extends React.Component {
26 constructor(props) {
27 super(props);
28 this.state = {
29 isOpen: false,
30 };
31 }
32
33 render() {
34 const { isOpen } = this.state;
35
36 return (
37 <TetherComponent
38 attachment="top center"
39 constraints={[
40 {
41 to: "scrollParent",
42 attachment: "together",
43 },
44 ]}
45 /* renderTarget: This is what the item will be tethered to, make sure to attach the ref */
46 renderTarget={(ref) => (
47 <button
48 ref={ref}
49 onClick={() => {
50 this.setState({ isOpen: !isOpen });
51 }}
52 >
53 Toggle Tethered Content
54 </button>
55 )}
56 /* renderElement: If present, this item will be tethered to the the component returned by renderTarget */
57 renderElement={(ref) =>
58 isOpen && (
59 <div ref={ref}>
60 <h2>Tethered Content</h2>
61 <p>A paragraph to accompany the title.</p>
62 </div>
63 )
64 }
65 />
66 );
67 }
68}
69```
70
71## Props
72
73#### `renderTarget`: PropTypes.func
74
75This is a [render prop](https://reactjs.org/docs/render-props.html), the component returned from this function will be Tether's `target`. One argument, ref, is passed into this function. This is a ref that must be attached to the highest possible DOM node in the tree. If this is not done the element will not render.
76
77#### `renderElement`: PropTypes.func
78
79This is a [render prop](https://reactjs.org/docs/render-props.html), the component returned from this function will be Tether's `element`, that will be moved. If no component is returned, the target will still render, but with no element tethered. One argument, ref, is passed into this function. This is a ref that must be attached to the highest possible DOM node in the tree. If this is not done the element will not render.
80
81#### `renderElementTag`: PropTypes.string
82
83The tag that is used to render the Tether element, defaults to `div`.
84
85#### `renderElementTo`: PropTypes.string
86
87Where in the DOM the Tether element is appended. Passes in any valid selector to `document.querySelector`. Defaults to `document.body`.
88
89Tether requires this element to be `position: static;`, otherwise it will default to `document.body`. See [this example](https://danreeves.github.io/react-tether/tests/renderelementto/) for more information.
90
91#### `Tether Options`:
92
93Any valid [Tether options](https://tetherjs.dev/#usage).
94
95#### `children`:
96
97Previous versions of react-tether used children to render the target and component, using children will now throw an error. Please use renderTarget and renderElement instead
98
99## Imperative API
100
101The following methods are exposed on the component instance:
102
103- `getTetherInstance(): Tether`
104- `disable(): void`
105- `enable(): void`
106- `on(event: string, handler: function, ctx: any): void`
107- `once(event: string, handler: function, ctx: any): void`
108- `off(event: string, handler: function): void`
109- `position(): void`
110
111#### Example usage:
112
113```javascript
114<TetherComponent
115 ref={(tether) => (this.tether = tether)}
116 renderTarget={(ref) => <Target ref={ref} />}
117 renderElement={(ref) => (
118 <Element ref={ref} onResize={() => this.tether && this.tether.position()} />
119 )}
120/>
121```
122
123## Run Example
124
125clone repo
126
127`git clone git@github.com:danreeves/react-tether.git`
128
129move into folder
130
131`cd ~/react-tether`
132
133install dependencies
134
135`npm install`
136
137run dev mode
138
139`npm run demo`
140
141open your browser and visit: `http://localhost:1234/`