Skip to content

Adding a title to the iframe tag #12

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,35 @@ Get the id from the gist url `https://gist.github.com/{your_name}/{id}` and set
Single-file gist:

```js
var React = require('react');
var Gist = require('react-gist');
var React = require("react");
var Gist = require("react-gist");

React.render(
<Gist id='5104372' />,
document.body
);
React.render(<Gist id="5104372" />, document.body);
```

[![Edit react-gist-example-single-file](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/reactgistexamplesinglefile-z9vkc?fontsize=14)

Multi-file gist:

```js
var React = require('react');
var Gist = require('react-gist');
var React = require("react");
var Gist = require("react-gist");

React.render(
<Gist id='5995ea726914f280afb3' file='Chef-Dockerfile' />,
document.body
<Gist id="5995ea726914f280afb3" file="Chef-Dockerfile" />,
document.body
);
```

[![Edit react-gist-example-multi-file](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/reactgistexamplemultifile-9rw4g?fontsize=14)

## Usage

### `<Gist id={string} file={string} />`
### `<Gist id={string} file={string} title={title} />`

- `id` {string} Id of the gist
- `file` {string} Name of a specific file in a multi-file gist
- `title` {title} Specific name of the <iframe> tag

## License

Expand Down
31 changes: 19 additions & 12 deletions demo/src/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
import React, {Component} from 'react'
import {render} from 'react-dom'
import React, { Component } from "react";
import { render } from "react-dom";

import Gist from '../../src'
import Gist from "../../src";

class Demo extends Component {
constructor(props) {
super(props);

this.state = {
id: 'bedde975e6e92a77e2321487ba45f313',
file: null
id: "bedde975e6e92a77e2321487ba45f313",
file: null,
title: null,
};
}

componentDidMount() {
// update the gist after 5 seconds
setTimeout(() => {
this.setState({id: '5995ea726914f280afb3', file: 'Chef-Dockerfile'});
this.setState({
id: "5995ea726914f280afb3",
file: "Chef-Dockerfile",
title: "Chef Dockerfile",
});
}, 5000);
}

render() {
return <div>
<h1>React-Gist</h1>
<p>The following gist will be updated in 5 seconds</p>
<Gist {...this.state} />
</div>
return (
<div>
<h1>React-Gist</h1>
<p>The following gist will be updated in 5 seconds</p>
<Gist {...this.state} />
</div>
);
}
}

render(<Demo/>, document.querySelector('#demo'))
render(<Demo />, document.querySelector("#demo"));
17 changes: 10 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import PropTypes from 'prop-types';
import React from "react";
import PropTypes from "prop-types";

class Gist extends React.PureComponent {
componentDidMount() {
Expand All @@ -13,7 +13,7 @@ class Gist extends React.PureComponent {
_defineUrl() {
const { id, file } = this.props;

const fileArg = file ? `?file=${file}` : '';
const fileArg = file ? `?file=${file}` : "";

return `https://gist.github.com/${id}.js${fileArg}`;
}
Expand All @@ -27,9 +27,9 @@ class Gist extends React.PureComponent {
if (iframe.contentDocument) doc = iframe.contentDocument;
else if (iframe.contentWindow) doc = iframe.contentWindow.document;

const gistLink = this._defineUrl()
const gistLink = this._defineUrl();
const gistScript = `<script type="text/javascript" src="${gistLink}"></script>`;
const styles = '<style>*{font-size:12px;}</style>';
const styles = "<style>*{font-size:12px;}</style>";
const elementId = file ? `gist-${id}-${file}` : `gist-${id}`;
const resizeScript = `onload="parent.document.getElementById('${elementId}').style.height=document.body.scrollHeight + 'px'"`;
const iframeHtml = `<html><head><base target="_parent">${styles}</head><body ${resizeScript}>${gistScript}</body></html>`;
Expand All @@ -40,14 +40,17 @@ class Gist extends React.PureComponent {
}

render() {
const { id, file } = this.props;
const { id, file, title } = this.props;

return (
<iframe
ref={(n) => { this.iframeNode = n; }}
ref={(n) => {
this.iframeNode = n;
}}
width="100%"
frameBorder={0}
id={file ? `gist-${id}-${file}` : `gist-${id}`}
title={title}
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PureComponent } from "react";
export interface GistProps {
id: string;
file?: string;
title?: string;
}

declare class Gist extends PureComponent<GistProps, any> {}
Expand Down