Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap examples in React.Fragment #840

Closed
sapegin opened this issue Feb 22, 2018 · 10 comments · Fixed by #842
Closed

Wrap examples in React.Fragment #840

sapegin opened this issue Feb 22, 2018 · 10 comments · Fixed by #842

Comments

@sapegin
Copy link
Member

sapegin commented Feb 22, 2018

This will allow to render multiple components without wrapping them in some <div>:

// Before
<div>
  <Button>push</Button>
  <Button secondary>push</Button>
</div>

// After
<Button>push</Button>
<Button secondary>push</Button>

We should keep React 15 compatibility and check if React.Fragment is available. Keep the existing behavior in React 15.

@tizmagik
Copy link
Collaborator

tizmagik commented Feb 23, 2018

Ah great idea! For React 15, is there harm in transparently wrapping in a div anyway so users wouldn’t have to explicitly wrap? 🤔 Something like:

const Fragment = React.Fragment || 'div';

return <Fragment>{example_here}</Fragment>

Haven’t looked at this in a while to see whether or not that makes sense, but seems doable.

@sapegin
Copy link
Member Author

sapegin commented Feb 23, 2018

Maybe not, if we make a major release. It may break some custom styles because of new added DOM element.

@sapegin sapegin added this to the 7.0.0 milestone Feb 23, 2018
@MarkMurphy
Copy link

This should probably be an overridable styleguidist component. This way, I can supply my own Example component that applies a flexbox layout and spacing around.

@sapegin
Copy link
Member Author

sapegin commented Mar 26, 2018

You can already do it with the Wrapper component.

@MarkMurphy
Copy link

MarkMurphy commented Mar 26, 2018

You can already do it with the Wrapper component.

How's that?

const Example = styled.div`
  display: flex;
  > * {
    margin: ${props => props.theme.spacing(1)};
  }
`;

const Wrapper = ({ children }) => (
  <ThemeProvider theme={theme}>
    <Example>{children}</Example>
  </ThemeProvider>
);

This:

```js
  <Button>Default</Button>
  <Button primary>Primary</Button>
  <Button secondary>Secondary</Button>
  <Button disabled>Disabled</Button>
```

Results in:

SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag (2:2)
1 :   <Button>Default</Button>
2 :   <Button primary>Primary</Button>
      ^

@MarkMurphy
Copy link

There's another component or two between the Wrapper and the content rendered from the code block so you can't just use the Wrapper.

sapegin added a commit that referenced this issue Mar 31, 2018
👋 **[Support Styleguidist](https://opencollective.com/styleguidist) on Open Collective** 👋

## Breaking changes

### Node 6 is the lowest supported version

Styleguidist doesn’t support Node 4 anymore.

(#744 #839 by @kohgpat)

### New format of template option

We’re now using [mini-html-webpack-plugin](https://github.com/styleguidist/mini-html-webpack-plugin) and [@vxna/mini-html-webpack-template](https://github.com/vxna/mini-html-webpack-template) instead of [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin).

This will make things like [adding a favicon](https://react-styleguidist.js.org/docs/cookbook#how-to-add-a-favicon) or fonts from [Google Fonts](https://react-styleguidist.js.org/docs/cookbook#how-to-add-fonts-from-google-fonts) much easier.

If you’re using a custom HTML template, you need to update your style guide config.

```js
// 6.x
module.exports = {
  template: './styleguie/template.html'
}

// 7.x
module.exports = {
  template: {
    // This is just an example, there are many more available options
    favicon: 'https://assets-cdn.github.com/favicon.ico'
  }
}
```

See more [in the docs](https://react-styleguidist.js.org/docs/configuration#template).

(#896 #907 by @sapegin)

### Changed Node API

`server` method now returns an object containing a webpack `Compiler` instance and the Styleguidist server, see examples [in the docs](https://react-styleguidist.js.org/docs/api.html#servercallback).

(#828 by @cmswalker)

## New features

### Webpack 4 support

Webpack 3 is still supported too.

(#857 by @kontrollanten, #900 #901 by @rubenmoya)

### Examples are wrapped in React.Fragment

You don’t need to wrap multiple JSX tags in a div anymore.

```jsx
// 6.x
<div>
  <Button primary>Primary button</Button>
  <Button secondary>Secondary button</Button>
</div>

// 7.x
<Button primary>Primary button</Button>
<Button secondary>Secondary button</Button>
```

(#840 #842 by @tizmagik)

## Bug fixes

* `components` option should accept arrays.
* Do not convert URLs in Markdown to auto links (#793).
* Improved accessibility (#893 by @gergely-nagy).

---

❤️ Huge thanks to @okonet to help with this release, [CI](#904) and [docs](#905) improvements.️ ❤️
@styleguidist-bot
Copy link
Collaborator

🎉 This issue has been resolved in version 7.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@joetidee
Copy link

joetidee commented Jun 27, 2018

Could also do this to avoid adding in unnecessary HTML tags:

const Fragment = React.Fragment || function(props) {return props.children};

return <Fragment>{example_here}</Fragment>

@mhelvens
Copy link

Any chance this could be made to work when the example code starts with some initialization?

initialState = { counter: 0 };

<Button onClick={() => setState({ counter: state.counter + 1 })}>Click Me</Button>
<span>{_.repeat(' ✓', state.counter)}</span>

This still gives me the Adjacent JSX elements must be wrapped in an enclosing tag error.

@sapegin
Copy link
Member Author

sapegin commented Nov 13, 2018

It's just a convenience, you can sill use fragments manually:

initialState = { counter: 0 };

<>
  <Button onClick={() => setState({ counter: state.counter + 1 })}>Click Me</Button>
  <span>{_.repeat(' ✓', state.counter)}</span>
</>

@styleguidist styleguidist locked as resolved and limited conversation to collaborators Nov 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants