Skip to content

Commit 3d73c7c

Browse files
committed
Merge branch 'add-className-and-style-to-wrapper'
2 parents 12cf4f4 + e6a6acf commit 3d73c7c

4 files changed

Lines changed: 12 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ For the definition of when each state is entered, see the [state machine definit
198198
| `focusToggleOff` | boolean | `focusToggleOff` | Add this prop to prevent focus from toggling on mouseup/tap. With this prop RI will enter the focus state normally and will remain in the focus state until the browser sends a blur event. |
199199
| `mutableProps` | boolean | `mutableProps` | Add this prop if you are passing in mutable props so the component will always update. By default it's assumed that props passed in are immutable. A shallow compare is done, and if the props are the same, the component will not update. If you're not sure and notice buggy behavior, then add this prop. |
200200
| `interactiveChild` | boolean | `interactiveChild` | Add this prop if Interactive's children use the [Interactive Children API](#interactive-children-api). |
201+
| `wrapperStyle` | style object | `{ display: 'block' }` | Styles that are applied to the `span` wrapper if `as` is a ReactComponent. |
202+
| `wrapperClassName` | string | `"ri-wrapper-class other-class"` | Classes that are applied to the `span` wrapper if `as` is a ReactComponent. |
201203
| `...` | anything | `id="some-id"`, `tabIndex="1"`, etc... | All additional props received are passed through. |
202204

203205
#### Merging Styles and Classes
@@ -222,6 +224,7 @@ For the definition of when each state is entered, see the [state machine definit
222224
- Strictly speaking this means that `as` is either a ReactClass or a ReactFunctionalComponent as defined in the [React Glossary](https://facebook.github.io/react/docs/glossary.html#classes-and-components).
223225
- In order for React Interactive to work `as` a ReactComponent, the component must pass down the props it receives from React Interactive to the top DOM node that it renders, and it cannot override any of the passed down event handlers, e.g. `onMouseEnter`. Also, the component cannot replace its top DOM node once it's rendered unless the replacement is the result of new props (note that mutations are okay, e.g. changing style, classes, children, etc is fine). This is because React Interactive keeps a reference to the component's top DOM node so it can do things like call `focus()`, and if the top DOM node is replaced without React Interactive's knowledge, then things start to break. Note that React Router's Link component meets these requirements.
224226
- When `as` is a ReactComponent it is wrapped in a `<span>` in order for React Interactive to maintain a reference to the top DOM node without breaking encapsulation. Without the span wrapper the only way to access the top DOM node would be through using `ReactDOM.findDOMNode(component)`, which breaks encapsulation and is discouraged, and also doesn't work with stateless functional components.
227+
- The `<span>` wrapper can be styled by passing down the props `wrapperClassName` (class string) and `wrapperStyle` (style object).
225228
- If `as` is a JSX/ReactElement:
226229
- E.g. `as={<div>...</div>}` or `as={<MyComponent />}`
227230
- The props of the top ReactElement are merged with, and have priority over, Interactive's props. For example:

src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export const knownProps = {
8787
as: true,
8888
style: true,
8989
className: true,
90+
wrapperStyle: true,
91+
wrapperClassName: true,
9092
onStateChange: true,
9193
setStateCallback: true,
9294
onClick: true,

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,11 @@ class Interactive extends React.Component {
14841484
// so can access the DOM node without breaking encapsulation
14851485
return React.createElement(
14861486
'span',
1487-
{ ref: this.refCallback },
1487+
{
1488+
ref: this.refCallback,
1489+
style: this.p.props.wrapperStyle,
1490+
className: this.p.props.wrapperClassName,
1491+
},
14881492
React.createElement(this.p.props.as, this.p.passThroughProps, children),
14891493
);
14901494
}

src/propTypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ const propTypes = {
6868
}),
6969
style: PropTypes.object,
7070
className: PropTypes.string,
71+
wrapperStyle: PropTypes.object,
72+
wrapperClassName: PropTypes.string,
7173
onStateChange: PropTypes.func,
7274
setStateCallback: PropTypes.func,
7375
onClick: PropTypes.func,

0 commit comments

Comments
 (0)