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

examples: add svg-components #982

Merged
merged 1 commit into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/svg-components/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": [ "next/babel" ],
"plugins": [ "inline-react-svg" ]
}
28 changes: 28 additions & 0 deletions examples/svg-components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

# SVG components example

## How to use

Download the example [or clone the repo](https://github.com/zeit/next.js):

```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/svg-components
cd svg-components
```

Install it and run:

```bash
npm install
npm run dev
```

Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))

```bash
now
```

## The idea behind the example

This example uses a custom `.babelrc` to add support for importing `.svg` files and rendering them as React components. [babel-plugin-inline-react-svg](https://www.npmjs.com/package/babel-plugin-inline-react-svg) is used to handle transpiling the SVGs.
15 changes: 15 additions & 0 deletions examples/svg-components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "svg-components",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "beta"
},
"devDependencies": {
"babel-plugin-inline-react-svg": "^0.2.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this is what we have been wanting ❤️ @rauchg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, ok! If you like, I can open another PR adding global support

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, but maybe inlining is not optimal in case a svg was imported from multiple files. We can use https://github.com/boopathi/react-svg-loader instead ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I had hopes for that one too. Unfortunately, I don't think it'll work, since webpack isn't used to build the server files :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hey, looks like there's a babel plugin there too. My observation skills may not be the best. I'll give it a shot soon =P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nkzawa if one needs a SVG in multiple places, one can simply create a file dedicated for it and the import that in multiple places.

E.g. a icons.js export multiple components which itself has imported through the babel transform

}
}
15 changes: 15 additions & 0 deletions examples/svg-components/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import Cat from '../svgs/cat.svg'

export default () => (
<div className='container'>
<marquee>SVG Cat!</marquee>
<Cat />
<style jsx>{`
.container {
width: 600px;
margin: 100px auto;
}
`}</style>
</div>
)
17 changes: 17 additions & 0 deletions examples/svg-components/svgs/cat.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.