-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from palmerhq/feature/stylesheet
Add Stylesheet, standardize errors, remove attribute copying, export element resources
- Loading branch information
Showing
16 changed files
with
158 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
|
||
export class ErrorBoundary extends React.Component { | ||
state = { error: null }; | ||
|
||
componentDidCatch(error) { | ||
this.setState({ error }); | ||
} | ||
reset() { | ||
this.setState({ error: null }); | ||
} | ||
render() { | ||
if (this.state.error !== null) { | ||
return <span>Error!</span>; | ||
} | ||
return this.props.children; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import React from 'react'; | ||
import { createResource } from 'simple-cache-provider'; | ||
import { cache, waitForReadyState } from './shared'; | ||
import { createCache, createResource } from 'simple-cache-provider'; | ||
import { isBrowser } from './utils'; | ||
|
||
export const audioCache = createCache(); | ||
export const AudioResource = createResource(load, ({ src }) => src); | ||
|
||
function load({ src }) { | ||
const audio = document.createElement('audio'); | ||
|
||
function load(attributes) { | ||
const { src, ...attrs } = attributes; | ||
return new Promise((resolve, reject) => { | ||
const audio = document.createElement('audio'); | ||
audio.oncanplay = resolve; | ||
audio.onerror = reject; | ||
audio.src = src; | ||
Object.keys(attrs).forEach(name => audio.setAttribute(name, attrs[name])); | ||
waitForReadyState(audio, resolve.bind(null, src)); | ||
}); | ||
} | ||
|
||
const resource = createResource(load, ({ src }) => src); | ||
export const Audio = props => { | ||
if (isBrowser) { | ||
AudioResource.read(audioCache, props); | ||
} | ||
|
||
export const Audio = ({ cache, ...props }) => { | ||
resource.read(cache, props); | ||
return <audio {...props} />; | ||
}; | ||
|
||
Audio.defaultProps = { | ||
cache, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import React from 'react'; | ||
import { createResource } from 'simple-cache-provider'; | ||
import { cache } from './shared'; | ||
import { createCache, createResource } from 'simple-cache-provider'; | ||
import { isBrowser } from './utils'; | ||
|
||
export const imgCache = createCache(); | ||
export const ImgResource = createResource(load, ({ src }) => src); | ||
|
||
function load({ src }) { | ||
const image = new Image(); | ||
|
||
function load(image) { | ||
const { src } = image; | ||
return new Promise((resolve, reject) => { | ||
const image = new Image(); | ||
image.onload = resolve; | ||
image.onerror = reject; | ||
image.src = src; | ||
}); | ||
} | ||
|
||
const resource = createResource(load, ({ src }) => src); | ||
export const Img = props => { | ||
if (isBrowser) { | ||
ImgResource.read(imgCache, props); | ||
} | ||
|
||
export const Img = ({ cache, ...props }) => { | ||
resource.read(cache, props); | ||
return <img {...props} />; | ||
}; | ||
|
||
Img.defaultProps = { | ||
cache, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import { createCache, createResource } from 'simple-cache-provider'; | ||
import { isBrowser } from './utils'; | ||
|
||
export const preloadCache = createCache(); | ||
export const PreloadResource = createResource( | ||
load, | ||
({ href, as }) => `${href}.${as}` | ||
); | ||
|
||
function load({ href, as, media = 'all' }) { | ||
const link = document.createElement('link'); | ||
link.rel = 'preload'; | ||
link.as = as; | ||
link.media = media; | ||
link.href = href; | ||
|
||
return new Promise((resolve, reject) => { | ||
link.onload = resolve; | ||
link.onerror = reject; | ||
document.body.appendChild(link); | ||
}); | ||
} | ||
|
||
export const Preload = ({ children, ...rest }) => { | ||
if (isBrowser) { | ||
PreloadResource.read(preloadCache, rest); | ||
} | ||
|
||
if (typeof children === 'function') { | ||
return children(); | ||
} | ||
|
||
return children; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import { createCache, createResource } from 'simple-cache-provider'; | ||
import { isBrowser } from './utils'; | ||
|
||
export const stylesheetCache = createCache(); | ||
export const StylesheetResource = createResource( | ||
load, | ||
({ href, media }) => `${href}.${media}` | ||
); | ||
|
||
function load({ href, media = 'all' }) { | ||
const link = document.createElement('link'); | ||
link.rel = 'stylesheet'; | ||
link.href = href; | ||
link.media = media; | ||
|
||
return new Promise((resolve, reject) => { | ||
link.onload = resolve; | ||
link.onerror = reject; | ||
document.body.appendChild(link); | ||
}); | ||
} | ||
|
||
export const Stylesheet = props => { | ||
if (isBrowser) { | ||
StylesheetResource.read(stylesheetCache, props); | ||
} | ||
|
||
return <link {...props} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import React from 'react'; | ||
import { createResource } from 'simple-cache-provider'; | ||
import { cache, waitForReadyState } from './shared'; | ||
import { createCache, createResource } from 'simple-cache-provider'; | ||
import { isBrowser } from './utils'; | ||
|
||
export const videoCache = createCache(); | ||
export const VideoResource = createResource(load, ({ src }) => src); | ||
|
||
function load({ src }) { | ||
const video = document.createElement('video'); | ||
|
||
function load(attributes) { | ||
const { src, ...attrs } = attributes; | ||
return new Promise((resolve, reject) => { | ||
const video = document.createElement('video'); | ||
video.oncanplay = resolve; | ||
video.onerror = reject; | ||
video.src = src; | ||
Object.keys(attrs).forEach(name => video.setAttribute(name, attrs[name])); | ||
waitForReadyState(video, resolve.bind(null, src)); | ||
}); | ||
} | ||
|
||
const resource = createResource(load, ({ src }) => src); | ||
export const Video = props => { | ||
if (isBrowser) { | ||
VideoResource.read(videoCache, props); | ||
} | ||
|
||
export const Video = ({ cache, ...props }) => { | ||
resource.read(cache, props); | ||
return <video {...props} />; | ||
}; | ||
|
||
Video.defaultProps = { | ||
cache, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from './Video'; | ||
export * from './Audio'; | ||
export * from './Img'; | ||
export * from './Preload'; | ||
export * from './Script'; | ||
export * from './Audio'; | ||
export * from './Stylesheet'; | ||
export * from './Video'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const isBrowser = typeof window !== 'undefined'; |