Skip to content

Commit

Permalink
Added in-built support for 'styled-components'
Browse files Browse the repository at this point in the history
  • Loading branch information
Wildhoney committed Jan 3, 2020
1 parent 131254e commit 637149b
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 254 deletions.
6 changes: 6 additions & 0 deletions example/js/components/Country/components/Image/index.js
@@ -0,0 +1,6 @@
import React from 'react';
import * as e from './styles';

export default function Image(props) {
return <e.Image {...props} />;
}
15 changes: 15 additions & 0 deletions example/js/components/Country/components/Image/styles.js
@@ -0,0 +1,15 @@
import styled from 'styled-components';

export const Image = styled.img`
width: 35vmin;
height: 35vmin;
@media (max-width: 285px) {
width: 20vmin;
height: 20vmin;
}
@media (max-width: 190px) {
display: none;
}
`;
7 changes: 5 additions & 2 deletions example/js/components/Country/index.js
Expand Up @@ -6,13 +6,16 @@ import DocumentTitle from 'react-document-title';
import * as duck from '../../duck';
import * as utils from '../../utils';
import Countries from '../Countries';
import Image from './components/Image';

function Country({ weather, country, countries, fetch }) {
const { title, label, fahrenheit, timezone, date } = utils.getWeather(
weather,
country,
);
useEffect(() => fetch(country), [country]);
useEffect(() => {
fetch(country);
}, [country]);

return (
<DocumentTitle title={`Weather for ${country}`}>
Expand All @@ -22,7 +25,7 @@ function Country({ weather, country, countries, fetch }) {
onClick={() => fetch(country, { cache: false })}
/>
<main>
<img src={utils.getFilename(country)} alt={country} />
<Image src={utils.getFilename(country)} alt={country} />
<h1>
{title} at{' '}
{timezone
Expand Down
13 changes: 0 additions & 13 deletions example/js/components/Layout/styles.css
Expand Up @@ -50,11 +50,6 @@ button.refresh:hover {
opacity: 0.5;
}

img {
width: 35vmin;
height: 35vmin;
}

h1 {
font-family: Times, 'Times New Roman', serif;
font-weight: normal;
Expand Down Expand Up @@ -137,15 +132,7 @@ select {
}
}

@media (max-width: 285px) {
img {
width: 20vmin;
height: 20vmin;
}
}

@media (max-width: 190px) {
img,
button {
display: none;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -74,6 +74,7 @@
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.1.3",
"sinon": "^8.0.2",
"styled-components": "^4.4.1",
"to-string-loader": "^1.1.6",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
Expand Down
20 changes: 19 additions & 1 deletion src/index.js
Expand Up @@ -3,6 +3,19 @@ import { createPortal } from 'react-dom';
import PropTypes from 'prop-types';
import { decamelize } from 'humps';

function Noop({ children }) {
return children;
}

function getStyleWrapper() {
try {
const styled = require('styled-components');
return styled.StyleSheetManager;
} catch {
return Noop;
}
}

function ShadowContent({ root, children }) {
return createPortal(children, root);
}
Expand All @@ -15,6 +28,7 @@ ShadowContent.propTypes = {
function createComponent(options) {
const ShadowRoot = forwardRef(
({ mode, delegatesFocus, styleSheets, children, ...props }, ref) => {
const Wrapper = getStyleWrapper();
const [node, setNode] = useState(null);
const [root, setRoot] = useState(null);

Expand All @@ -34,7 +48,11 @@ function createComponent(options) {
return (
<options.tag ref={setNode} {...props}>
{root && (
<ShadowContent root={root}>{children}</ShadowContent>
<Wrapper target={root}>
<ShadowContent root={root}>
{children}
</ShadowContent>
</Wrapper>
)}
</options.tag>
);
Expand Down

0 comments on commit 637149b

Please sign in to comment.