Skip to content

Commit 227581a

Browse files
committed
feat: modernize the library
1 parent 1722a88 commit 227581a

File tree

6 files changed

+26294
-9495
lines changed

6 files changed

+26294
-9495
lines changed

CONTRIBUTING.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010

1111
- `npm start` will run the app's demo folder at [http://localhost:3000](http://localhost:3000) with hot module reloading.
1212

13-
## Running Tests
14-
15-
- `npm test` will run the tests once.
16-
17-
- `npm run test:coverage` will run the tests and produce a coverage report in `coverage/`.
18-
19-
- `npm run test:watch` will run the tests on every change.
20-
2113
## Building
2214

2315
- `npm run build` creates a production build by default.

README.md

Lines changed: 119 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
11
# super-react-gist
22
Simple and flexible component that allows you to embed [GitHub Gists](https://gist.github.com/) in [React](https://reactjs.org/) projects.
33

4-
54
## Features
65
- Embed a **single file** from Gist repository!
76
- Embed **multiple files** from multiple Gist repositories!
87
- Embed the **whole Gist** repository!
98
- **Easy usage**: Just copy-paste the Gist's repository URL to embed the whole repository, or copy-paste the permalink of an individual file to include just that.
9+
- Maximize Development Experience with **custom error handling**.
1010
- **Lightweight**: ~9KB minified (~4kb if gzipped).
1111
- Packaged as **UMD module** that can be loaded everywhere.
1212
- **Works both on secret and public repositories**.
1313

1414
## Table of contents
15-
- [Quick Start](#quick-start)
16-
- [Usage](#usage)
15+
- [Installation](#installation)
16+
- [Component Properties](#component-properties)
1717
- [Examples](#examples)
1818
- [Browser Support](#browser-support)
1919
- [Bugs and feature requests](#bugs-and-feature-requests)
2020
- [Contributing](#contributing)
21-
- [Creator](#creator)
22-
- [Licence](#license)
21+
- [License](#license)
2322

24-
## Quick Start
25-
- Clone the repo: `git clone https://github.com/georgegkas/super-react-gist.git`
26-
- Install with [npm](https://www.npmjs.com/): `npm install --save super-react-gist`
23+
## Installation
24+
### Through NPM
25+
To install through npm run:
2726

28-
## Usage
29-
**`<Gist url={string} file={string} />`**
30-
- `url` *{ string }* **required** The URL of the Gist repository or the permalink of an individual gist file.
31-
- `file` *{ string }* Name of a specific file to include.
27+
```bash
28+
npm i super-react-gist
29+
```
30+
### As UMD module
31+
**super-react-gist** comes as UMD module. This means you are able to use **super-react-gist** component in your browser!
3232

33+
To get started add the following script tag in your html file:
34+
35+
```html
36+
<script src="https://unpkg.com/super-react-gist/umd/super-react-gist.min.js"></script>
37+
```
38+
39+
## Component Properties
40+
41+
| Name | Type | Required | Description |
42+
| :--- | :--- | :---: | :--- |
43+
| `url` | *string* || The URL of the Gist repository or the permalink of an individual gist file. |
44+
| `file` | *string* | | Optional filename to include. |
45+
| `onLoad` | *function* | | Optional callback triggered on Gist load. |
46+
| `onError` | *function* | | Optional callback triggered on fetch error. |
47+
| `LoadingComponent` | *Component* | | Optional React component to render on Gist loading. |
48+
| `ErrorComponent` | *Component* || Optional React component to render if Gist fetch fails. |
3349

3450
## Examples
35-
The following examples illustrate some basic usages of **super-react-gist** component.
51+
The following examples illustrate some basic features of the **super-react-gist** library.
3652

3753
### Render one file
3854
With **super-react-gist** you are able to render a single file from a gist repository.
@@ -42,36 +58,28 @@ With **super-react-gist** you are able to render a single file from a gist repos
4258
import React from 'react'
4359
import Gist from 'super-react-gist' // <-- import the library
4460

45-
class MyComponent extends React.Component {
46-
render() {
47-
return (
48-
<div>
49-
<p>Just enter the file permalink to <em>url</em> prop.</p>
50-
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-start-js' />
51-
</div>
52-
)
53-
}
54-
}
61+
const MyComponent = () => (
62+
<div>
63+
<p>Just enter the file permalink to <em>url</em> prop.</p>
64+
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-start-js' />
65+
</div>
66+
)
5567
```
5668

5769
### Render multiple files
58-
You are not restricted to use only one **super-react-gist** component in your project.
70+
You are not restricted to use only one **Gist** component in your project.
5971

6072
```javascript
6173
import React from 'react'
6274
import Gist from 'super-react-gist' // <-- import the library
6375

64-
class MyComponent extends React.Component {
65-
render() {
66-
return (
67-
<div>
68-
<p>Rendering multiple files is a piece of cake!</p>
69-
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-start-js' />
70-
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-multiple-js' />
71-
</div>
72-
)
73-
}
74-
}
76+
const MyComponent = () => (
77+
<div>
78+
<p>Rendering multiple files is a piece of cake!</p>
79+
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-start-js' />
80+
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-multiple-js' />
81+
</div>
82+
)
7583
```
7684

7785
### Render using `file` prop
@@ -86,20 +94,16 @@ This is how we do it:
8694
import React from 'react'
8795
import Gist from 'super-react-gist'
8896

89-
class MyComponent extends React.Component {
90-
render() {
91-
return (
92-
<div>
93-
<p>provide the Gist url without including the file.</p>
94-
<p>...and pass the filename to `file` prop.</p>
95-
<Gist
96-
url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df'
97-
file='CaMelCase.js'
98-
/>
99-
</div>
100-
)
101-
}
102-
}
97+
const MyComponent = () => (
98+
<div>
99+
<p>provide the Gist url without including the file.</p>
100+
<p>...and pass the filename to `file` prop.</p>
101+
<Gist
102+
url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df'
103+
file='CaMelCase.js'
104+
/>
105+
</div>
106+
)
103107
```
104108

105109
### Render the whole Gist
@@ -109,40 +113,82 @@ Of course, we can also embed the whole Gist repository just by copying the Gist
109113
import React from 'react'
110114
import Gist from 'super-react-gist'
111115

112-
class MyComponent extends React.Component {
113-
render() {
114-
return (
115-
<div>
116-
<p>provide the Gist URL without include any file.</p>
117-
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df' />
118-
</div>
119-
)
120-
}
121-
}
116+
const MyComponent = () => (
117+
<div>
118+
<p>provide the Gist URL without include any file.</p>
119+
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df' />
120+
</div>
121+
)
122+
```
123+
124+
### Use a custom loading component
125+
Most of the times, we would like to render a custom React component while our Gists are loading.
126+
127+
```javascript
128+
import React from 'react'
129+
import Gist from 'super-react-gist'
130+
131+
const MyComponent = () => (
132+
<div>
133+
<p>provide the Gist URL without include any file.</p>
134+
<Gist
135+
url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df'
136+
LoadingComponent={() => <div>Waiting for Gist...</div>}
137+
/>
138+
</div>
139+
)
140+
```
141+
142+
### Use a custom error component
143+
In case that fetching fails, we can render a custom React Component to indicate the error.
144+
145+
```javascript
146+
import React from 'react'
147+
import Gist from 'super-react-gist'
148+
149+
const MyComponent = () => (
150+
<div>
151+
<p>provide the Gist URL without include any file.</p>
152+
<Gist
153+
url='https://gist.github.com/GeorgeGkas/NOT_EXIST'
154+
ErrorComponent={() => <div>Could not fetch component</div>}
155+
/>
156+
</div>
157+
)
158+
```
159+
160+
### Listen to error and loading events
161+
Apart from providing a custom error or loading component, we can also register the corresponding callbacks. The `onLoad` callback is executed when the Gist is fetched successfully, while `onError` callback is executed if could not retrieve the requested Gist.
162+
163+
```javascript
164+
import React from 'react'
165+
import Gist from 'super-react-gist'
166+
167+
const MyComponent = () => (
168+
<div>
169+
<p>provide the Gist URL without include any file.</p>
170+
<Gist
171+
url='https://gist.github.com/GeorgeGkas/NOT_EXIST'
172+
οnLoad={() => console.log('Gist fetched successfully!')}
173+
onError={() => console.log('Gist could not be fetched!')}
174+
/>
175+
</div>
176+
)
122177
```
123178

124179
### Combine all the above techniques
125180
*This example is left as an exercise to the reader.*
126181

127182

128183
### Run the examples yourself
129-
Clone the repository and then run:
184+
Clone the repo `git clone https://github.com/georgegkas/super-react-gist.git` and then run:
185+
130186
```
131187
$ npm install
132-
$ npm run build
133188
$ npm start
134189
```
135190

136-
## Browser Support
137-
**super-react-gist** comes as UMD module. This means you are able to use **super-react-gist** component in your browser!
138-
139-
To get started add the following script tag in your html file:
140-
141-
```html
142-
<script src="https://unpkg.com/super-react-gist/umd/super-react-gist.min.js"></script>
143-
```
144-
145-
Then you are able to access **super-react-gist** component using the `Gist` name, which is the global variable the UMD build has exported. [See this Pen for a demonstration](https://codepen.io/georgegkas/pen/zpzMzz).
191+
Then you are able to access the `Gist` component using the `Gist` global variable. [See this Pen for a demonstration](https://codepen.io/georgegkas/pen/zpzMzz).
146192

147193
## Bugs and feature requests
148194
Have a bug or a feature request? [Please open a new issue](https://github.com/georgegkas/super-react-gist/issues).
@@ -152,11 +198,5 @@ Please read through our contributing guidelines in [CONTRIBUTING.md](https://git
152198

153199
Editor preferences are available in the editor config for easy use in common text editors. Read more and download plugins at [http://editorconfig.org](http://editorconfig.org).
154200

155-
## Creator
156-
George Gkasdrogkas
157-
158-
- [https://github.com/georgegkas](https://github.com/georgegkas)
159-
- [https://codepen.io/georgegkas](http://codepen.io/georgegkas)
160-
161-
## Licence
162-
Code released under the [MIT License](https://opensource.org/licenses/MIT). See [LICENSE.md](https://github.com/georgegkas/super-react-gist/blob/master/LICENSE.md) for more details.
201+
## License
202+
Code released under the [MIT License](https://opensource.org/licenses/MIT). See [LICENSE.md](https://github.com/georgegkas/super-react-gist/blob/master/LICENSE.md) for more details.

demo/src/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import ReactDom from 'react-dom'
33

4-
import Gist from '../../src'
4+
import Gist from '../../src/index'
55

66
const Demo = () => (
77
<div>
@@ -10,24 +10,28 @@ const Demo = () => (
1010
<p>This demo illustrate a basic usage of <strong>super-react-app</strong> component.</p>
1111
<h2>Render one file</h2>
1212
<p>With <strong>super-react-app</strong> you are able to render a single file from a gist repository.</p>
13-
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-start-js' />
13+
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-start-js' LoadingComponent={() => <strong>Custom loading component...</strong>} />
1414
<h2>Render multiple files</h2>
1515
<p>You are not restricted to use only one <strong>super-react-app</strong> component in your project. </p>
16-
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-start-js' />
17-
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-multiple-js' />
16+
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-start-js' LoadingComponent={() => <strong>Custom loading component...</strong>} />
17+
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df#file-multiple-js' LoadingComponent={() => <strong>Custom loading component...</strong>} />
1818
<h2>Render using <code>file</code> prop</h2>
1919
<p>Works both on <strong>public</strong> and <strong>secret</strong> gists.</p>
2020
<p>Oh snap! *face-palm*! In case you didn't notice we can only use the above method to render files that do not contain any uppercase letter. For instance, if our Gist repo contains a file <code>CaMelCase.js</code>, then providing just the permalink will not work!</p>
2121
<p><strong>Q</strong>: How can we render this <code>CaMelCase.js</code> file?</p>
2222
<p><strong>A</strong>: By providing a <code>file</code> prop to our <code>Gist</code> component to indicate which file we want to include.</p>
2323
<p>This is how we do it:</p>
24-
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df' file='CaMelCase.js' />
24+
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df' file='CaMelCase.js' LoadingComponent={() => <strong>Custom loading component...</strong>} />
2525
<h2>Render the whole Gist</h2>
2626
<p>Of course, we can also embed the whole Gist repository just by copying the Gist URL.</p>
27-
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df' />
27+
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b766934ffe802d30df' LoadingComponent={() => <strong>Custom loading component...</strong>} />
28+
29+
<h2>Error Component</h2>
30+
<p>Render a custom error component if Gist could not be retrieved.</p>
31+
<Gist url='https://gist.github.com/GeorgeGkas/5f55a83909a3f5b7tyjty66934ffedd802d30df#file-multiple-js' LoadingComponent={() => <strong>Custom loading component...</strong>} ErrorComponent={() => <strong>Gist failed to load.</strong>} />
32+
2833
<h2>Combine all the above techniques</h2>
2934
<p><em>This example is left as an exercise to the reader.</em></p>
30-
{/* Type here */}
3135
</div>
3236
)
3337

0 commit comments

Comments
 (0)