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

feat: Add 'scope' option for providing custom scope variables #201

Merged
merged 2 commits into from
Oct 12, 2020
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
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ module.exports = {
themes: './src/themes',
snippets: './playroom/snippets.js',
frameComponent: './playroom/FrameComponent.js',
widths: [320, 375, 768, 1024],
scope: './playroom/useScope.js',
widths: [320, 768, 1024],
port: 9000,
openBrowser: true,
paramType: 'search', // default is 'hash'
Expand Down Expand Up @@ -124,11 +125,24 @@ If your components need to be nested within custom provider components, you can

```js
import React from 'react';
import ThemeProvider from '../path/to/your/ThemeProvider';
import { ThemeProvider } from '../path/to/your/theming-system';

export default ({ theme, children }) => (
<ThemeProvider theme={theme}>{children}</ThemeProvider>
);
export default function FrameComponent({ theme, children }) {
return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
}
```

## Custom Scope

You can provide extra variables within the scope of your JSX via the `scope` option, which is a path to a file that exports a `useScope` Hook that returns a scope object. For example, if you wanted to expose a context-based `theme` variable to consumers of your Playroom:

```js
import { useTheme } from '../path/to/your/theming-system';

export default function useScope() {
return {
theme: useTheme(),
Copy link
Contributor

Choose a reason for hiding this comment

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

@markdalgleish this is great!

How does this get used? like is it a magical object that is accessible in the code pane? Ie can we scope.XYZ and use that? Or what is a usecase for this?

};
```

## Theme Support
Expand Down
16 changes: 16 additions & 0 deletions cypress/integration/scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
typeCode,
assertFirstFrameContains,
loadPlayroom,
} from '../support/utils';

describe('useScope', () => {
beforeEach(() => {
loadPlayroom();
});

it('works', () => {
typeCode('{{}hello()} {{}world()}', { delay: 0 });
assertFirstFrameContains('HELLO WORLD');
});
});
1 change: 1 addition & 0 deletions cypress/projects/basic/playroom.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
components: './components',
scope: './useScope',
snippets: './snippets',
outputPath: './dist',
openBrowser: false,
Expand Down
4 changes: 4 additions & 0 deletions cypress/projects/basic/useScope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default () => ({
hello: () => 'HELLO',
world: () => 'WORLD',
});
4 changes: 2 additions & 2 deletions cypress/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export const visit = (url) =>
);
});

export const typeCode = (code) =>
export const typeCode = (code, { delay = 200 } = {}) =>
getCodeEditor()
.focused()
.type(code, { force: true, delay: 200 })
.type(code, { force: true, delay })
.wait(WAIT_FOR_FRAME_TO_RENDER);

export const formatCode = () =>
Expand Down
1 change: 1 addition & 0 deletions lib/defaultModules/useScope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => {};
3 changes: 3 additions & 0 deletions lib/makeWebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ module.exports = async (playroomConfig, options) => {
__PLAYROOM_ALIAS__FRAME_COMPONENT__: playroomConfig.frameComponent
? relativeResolve(playroomConfig.frameComponent)
: require.resolve('./defaultModules/FrameComponent'),
__PLAYROOM_ALIAS__USE_SCOPE__: playroomConfig.scope
? relativeResolve(playroomConfig.scope)
: require.resolve('./defaultModules/useScope'),
},
},
module: {
Expand Down
37 changes: 9 additions & 28 deletions src/Playroom/RenderCode/RenderCode.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import React from 'react';
import scopeEval from 'scope-eval';

export default class RenderCode extends Component {
static displayName = 'RenderCode';
// eslint-disable-next-line import/no-unresolved
import useScope from '__PLAYROOM_ALIAS__USE_SCOPE__';

static propTypes = {
code: PropTypes.string.isRequired,
scope: PropTypes.object,
initialState: PropTypes.object,
};

static defaultProps = {
scope: {},
initialState: {},
};

constructor(props) {
super(props);

this.state = this.props.initialState;
}

render() {
const { code, scope } = this.props;

const el = scopeEval(code, { ...scope, React, this: this });

return el;
}
export default function RenderCode({ code, scope }) {
return scopeEval(code, {
...(useScope() ?? {}),
...scope,
React,
});
}
4 changes: 4 additions & 0 deletions src/useScope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable-next-line import/no-unresolved */
const useScope = require('__PLAYROOM_ALIAS__USE_SCOPE__');

module.exports = useScope.default || useScope;