1 | # rc-trigger
|
2 | ---
|
3 |
|
4 | React Trigger Component
|
5 |
|
6 | [![NPM version][npm-image]][npm-url]
|
7 | [![build status][github-actions-image]][github-actions-url]
|
8 | [![Test coverage][coveralls-image]][coveralls-url]
|
9 | [![Dependencies][david-image]][david-url]
|
10 | [![DevDependencies][david-dev-image]][david-dev-url]
|
11 | [![npm download][download-image]][download-url]
|
12 | [![bundle size][bundlephobia-image]][bundlephobia-url]
|
13 | [![dumi][dumi-image]][dumi-url]
|
14 |
|
15 | [npm-image]: http://img.shields.io/npm/v/rc-trigger.svg?style=flat-square
|
16 | [npm-url]: http://npmjs.org/package/rc-trigger
|
17 | [github-actions-image]: https://github.com/react-component/trigger/workflows/CI/badge.svg
|
18 | [github-actions-url]: https://github.com/react-component/trigger/actions
|
19 | [circleci-image]: https://img.shields.io/circleci/react-component/trigger/master?style=flat-square
|
20 | [circleci-url]: https://circleci.com/gh/react-component/trigger
|
21 | [coveralls-image]: https://img.shields.io/coveralls/react-component/trigger.svg?style=flat-square
|
22 | [coveralls-url]: https://coveralls.io/r/react-component/trigger?branch=master
|
23 | [david-url]: https://david-dm.org/react-component/trigger
|
24 | [david-image]: https://david-dm.org/react-component/trigger/status.svg?style=flat-square
|
25 | [david-dev-url]: https://david-dm.org/react-component/trigger?type=dev
|
26 | [david-dev-image]: https://david-dm.org/react-component/trigger/dev-status.svg?style=flat-square
|
27 | [download-image]: https://img.shields.io/npm/dm/rc-trigger.svg?style=flat-square
|
28 | [download-url]: https://npmjs.org/package/rc-trigger
|
29 | [bundlephobia-url]: https://bundlephobia.com/result?p=rc-trigger
|
30 | [bundlephobia-image]: https://badgen.net/bundlephobia/minzip/rc-trigger
|
31 | [dumi-image]: https://img.shields.io/badge/docs%20by-dumi-blue?style=flat-square
|
32 | [dumi-url]: https://github.com/umijs/dumi
|
33 |
|
34 | ## Install
|
35 |
|
36 | [![rc-trigger](https://nodei.co/npm/rc-trigger.png)](https://npmjs.org/package/rc-trigger)
|
37 |
|
38 | ## Usage
|
39 |
|
40 | Include the default [styling](https://github.com/react-component/trigger/blob/master/assets/index.less#L4:L11) and then:
|
41 |
|
42 | ```js
|
43 | import React from 'react';
|
44 | import ReactDOM from 'react-dom';
|
45 | import Trigger from 'rc-trigger';
|
46 |
|
47 | ReactDOM.render((
|
48 | <Trigger
|
49 | action={['click']}
|
50 | popup={<span>popup</span>}
|
51 | popupAlign={{
|
52 | points: ['tl', 'bl'],
|
53 | offset: [0, 3]
|
54 | }}
|
55 | >
|
56 | <a href='#'>hover</a>
|
57 | </Trigger>
|
58 | ), container);
|
59 | ```
|
60 |
|
61 | ## Compatibility
|
62 |
|
63 | | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="IE / Edge" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>IE / Edge | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png" alt="Firefox" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Firefox | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Chrome | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png" alt="Safari" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Safari | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/electron/electron_48x48.png" alt="Electron" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Electron |
|
64 | | --- | --- | --- | --- | --- |
|
65 | | IE11, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
|
66 |
|
67 | ## Example
|
68 |
|
69 | http://localhost:9001
|
70 |
|
71 | online example: http://react-component.github.io/trigger/
|
72 |
|
73 | ## Development
|
74 |
|
75 | ```
|
76 | npm install
|
77 | npm start
|
78 | ```
|
79 |
|
80 | ## API
|
81 |
|
82 | ### props
|
83 |
|
84 | <table class="table table-bordered table-striped">
|
85 | <thead>
|
86 | <tr>
|
87 | <th style="width: 100px;">name</th>
|
88 | <th style="width: 50px;">type</th>
|
89 | <th style="width: 50px;">default</th>
|
90 | <th>description</th>
|
91 | </tr>
|
92 | </thead>
|
93 | <tbody>
|
94 | <tr>
|
95 | <td>alignPoint</td>
|
96 | <td>bool</td>
|
97 | <td>false</td>
|
98 | <td>Popup will align with mouse position (support action of 'click', 'hover' and 'contextMenu')</td>
|
99 | </tr>
|
100 | <tr>
|
101 | <td>popupClassName</td>
|
102 | <td>string</td>
|
103 | <td></td>
|
104 | <td>additional className added to popup</td>
|
105 | </tr>
|
106 | <tr>
|
107 | <td>forceRender</td>
|
108 | <td>boolean</td>
|
109 | <td>false</td>
|
110 | <td>whether render popup before first show</td>
|
111 | </tr>
|
112 | <tr>
|
113 | <td>destroyPopupOnHide</td>
|
114 | <td>boolean</td>
|
115 | <td>false</td>
|
116 | <td>whether destroy popup when hide</td>
|
117 | </tr>
|
118 | <tr>
|
119 | <td>getPopupClassNameFromAlign</td>
|
120 | <td>getPopupClassNameFromAlign(align: Object):String</td>
|
121 | <td></td>
|
122 | <td>additional className added to popup according to align</td>
|
123 | </tr>
|
124 | <tr>
|
125 | <td>action</td>
|
126 | <td>string[]</td>
|
127 | <td>['hover']</td>
|
128 | <td>which actions cause popup shown. enum of 'hover','click','focus','contextMenu'</td>
|
129 | </tr>
|
130 | <tr>
|
131 | <td>mouseEnterDelay</td>
|
132 | <td>number</td>
|
133 | <td>0</td>
|
134 | <td>delay time to show when mouse enter. unit: s.</td>
|
135 | </tr>
|
136 | <tr>
|
137 | <td>mouseLeaveDelay</td>
|
138 | <td>number</td>
|
139 | <td>0.1</td>
|
140 | <td>delay time to hide when mouse leave. unit: s.</td>
|
141 | </tr>
|
142 | <tr>
|
143 | <td>popupStyle</td>
|
144 | <td>Object</td>
|
145 | <td></td>
|
146 | <td>additional style of popup</td>
|
147 | </tr>
|
148 | <tr>
|
149 | <td>prefixCls</td>
|
150 | <td>String</td>
|
151 | <td>rc-trigger-popup</td>
|
152 | <td>prefix class name</td>
|
153 | </tr>
|
154 | <tr>
|
155 | <td>popupTransitionName</td>
|
156 | <td>String|Object</td>
|
157 | <td></td>
|
158 | <td>https://github.com/react-component/animate</td>
|
159 | </tr>
|
160 | <tr>
|
161 | <td>maskTransitionName</td>
|
162 | <td>String|Object</td>
|
163 | <td></td>
|
164 | <td>https://github.com/react-component/animate</td>
|
165 | </tr>
|
166 | <tr>
|
167 | <td>onPopupVisibleChange</td>
|
168 | <td>Function</td>
|
169 | <td></td>
|
170 | <td>call when popup visible is changed</td>
|
171 | </tr>
|
172 | <tr>
|
173 | <td>mask</td>
|
174 | <td>boolean</td>
|
175 | <td>false</td>
|
176 | <td>whether to support mask</td>
|
177 | </tr>
|
178 | <tr>
|
179 | <td>maskClosable</td>
|
180 | <td>boolean</td>
|
181 | <td>true</td>
|
182 | <td>whether to support click mask to hide</td>
|
183 | </tr>
|
184 | <tr>
|
185 | <td>popupVisible</td>
|
186 | <td>boolean</td>
|
187 | <td></td>
|
188 | <td>whether popup is visible</td>
|
189 | </tr>
|
190 | <tr>
|
191 | <td>zIndex</td>
|
192 | <td>number</td>
|
193 | <td></td>
|
194 | <td>popup's zIndex</td>
|
195 | </tr>
|
196 | <tr>
|
197 | <td>defaultPopupVisible</td>
|
198 | <td>boolean</td>
|
199 | <td></td>
|
200 | <td>whether popup is visible initially</td>
|
201 | </tr>
|
202 | <tr>
|
203 | <td>popupAlign</td>
|
204 | <td>Object: alignConfig of [dom-align](https://github.com/yiminghe/dom-align)</td>
|
205 | <td></td>
|
206 | <td>popup 's align config</td>
|
207 | </tr>
|
208 | <tr>
|
209 | <td>onPopupAlign</td>
|
210 | <td>function(popupDomNode, align)</td>
|
211 | <td></td>
|
212 | <td>callback when popup node is aligned</td>
|
213 | </tr>
|
214 | <tr>
|
215 | <td>popup</td>
|
216 | <td>React.Element | function() => React.Element</td>
|
217 | <td></td>
|
218 | <td>popup content</td>
|
219 | </tr>
|
220 | <tr>
|
221 | <td>getPopupContainer</td>
|
222 | <td>getPopupContainer(): HTMLElement</td>
|
223 | <td></td>
|
224 | <td>function returning html node which will act as popup container</td>
|
225 | </tr>
|
226 | <tr>
|
227 | <td>getDocument</td>
|
228 | <td>getDocument(): HTMLElement</td>
|
229 | <td></td>
|
230 | <td>function returning document node which will be attached click event to close trigger</td>
|
231 | </tr>
|
232 | <tr>
|
233 | <td>popupPlacement</td>
|
234 | <td>string</td>
|
235 | <td></td>
|
236 | <td>use preset popup align config from builtinPlacements, can be merged by popupAlign prop</td>
|
237 | </tr>
|
238 | <tr>
|
239 | <td>builtinPlacements</td>
|
240 | <td>object</td>
|
241 | <td></td>
|
242 | <td>builtin placement align map. used by placement prop</td>
|
243 | </tr>
|
244 | <tr>
|
245 | <td>stretch</td>
|
246 | <td>string</td>
|
247 | <td></td>
|
248 | <td>Let popup div stretch with trigger element. enums of 'width', 'minWidth', 'height', 'minHeight'. (You can also mixed with 'height minWidth')</td>
|
249 | </tr>
|
250 | </tbody>
|
251 | </table>
|
252 |
|
253 |
|
254 | ## Test Case
|
255 |
|
256 | ```
|
257 | npm test
|
258 | npm run chrome-test
|
259 | ```
|
260 |
|
261 | ## Coverage
|
262 |
|
263 | ```
|
264 | npm run coverage
|
265 | ```
|
266 |
|
267 | open coverage/ dir
|
268 |
|
269 | ## React 16 Note
|
270 |
|
271 | After React 16, you won't access popup element's ref in parent component's componentDidMount, which means following code won't work.
|
272 |
|
273 | ```javascript
|
274 | class App extends React.Component {
|
275 | componentDidMount() {
|
276 | this.input.focus(); // error, this.input is undefined.
|
277 | }
|
278 |
|
279 | render() {
|
280 | return (
|
281 | <Trigger
|
282 | action={['click']}
|
283 | popup={<div><input ref={node => this.input = node} type="text" /></div>}
|
284 | >
|
285 | <button>click</button>
|
286 | </Trigger>
|
287 | )
|
288 | }
|
289 | }
|
290 | ```
|
291 |
|
292 | Consider wrap your popup element to a separate component:
|
293 |
|
294 | ```javascript
|
295 | class InputPopup extends React.Component {
|
296 | componentDidMount() {
|
297 | this.props.onMount();
|
298 | }
|
299 |
|
300 | render() {
|
301 | return (
|
302 | <div>
|
303 | <input ref={this.props.inputRef} type="text" />
|
304 | </div>
|
305 | );
|
306 | }
|
307 | }
|
308 |
|
309 | class App extends React.Component {
|
310 | handlePopupMount() {
|
311 | this.input.focus(); // error, this.input is undefined.
|
312 | }
|
313 |
|
314 | render() {
|
315 | return (
|
316 | <Trigger
|
317 | action={['click']}
|
318 | popup={<InputPopup inputRef={node => this.input = node} onMount={this.handlePopupMount} />}
|
319 | >
|
320 | <button>click</button>
|
321 | </Trigger>
|
322 | )
|
323 | }
|
324 | }
|
325 | ```
|
326 |
|
327 | ## License
|
328 |
|
329 | rc-trigger is released under the MIT license.
|