Skip to content

Commit

Permalink
fix: replace onRender effect
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 19, 2020
1 parent 6acd915 commit 9269694
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 53 deletions.
14 changes: 10 additions & 4 deletions plugins/axe-plugin/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const AxeAllyBlock: FC<AxeAllyBlockProps> = ({
resetTabCounter();
runAxe(canvas)
.then(results => {
console.log(results);
setResults(results);
isRunning.current = false;
})
Expand All @@ -48,7 +49,9 @@ export const AxeAllyBlock: FC<AxeAllyBlockProps> = ({
}
};
const onRender = () => {
collectResults();
try {
collectResults();
} catch (e) {}
};

return (
Expand All @@ -57,9 +60,12 @@ export const AxeAllyBlock: FC<AxeAllyBlockProps> = ({
<>
<React.Suspense fallback={<div>testing...</div>}>
<BaseAllyBlock options={axeOptions}>
<React.Profiler id="axe-plugin-scan" onRender={onRender}>
<Story key={storyId} id={storyId} ref={storyRef} />
</React.Profiler>
<Story
key={storyId}
id={storyId}
ref={storyRef}
onRender={onRender}
/>
</BaseAllyBlock>
</React.Suspense>
</>
Expand Down
88 changes: 46 additions & 42 deletions ui/blocks/src/Story/Story.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @jsx jsx */
import { FC, createElement, forwardRef } from 'react';
import { FC, createElement, forwardRef, useEffect } from 'react';
import { jsx, Box } from 'theme-ui';
import { deepMerge, StoryRenderFn } from '@component-controls/specification';
import { getControlValues } from '@component-controls/core';
Expand All @@ -10,55 +10,59 @@ import {

export type StoryProps = StoryBlockContainerProps & {
ref?: React.Ref<HTMLDivElement>;
onRender?: () => void;
};

/**
* block component to render story function with decorators
*/
export const Story: FC<StoryProps> = forwardRef(
(props: StoryProps, ref: React.Ref<HTMLDivElement>) => (
<StoryBlockContainer {...props}>
{(context, rest) => {
const { story, options = {} } = context;
if (story && story.renderFn) {
try {
const values = getControlValues(story.controls);
const { decorators: globalDecorators = [] } = options;
const { decorators: storyDecorators = [] } = story;
const decorators = deepMerge(globalDecorators, storyDecorators);
const renderFn = decorators.reduce(
(acc: StoryRenderFn, item: StoryRenderFn) => () =>
item(acc, { context }),
//@ts-ignore
() => story.renderFn(values, { context }),
);
return (
<Box
id={story.id}
sx={{
px: 4,
py: 3,
}}
{...rest}
>
<div
className="story-render-container"
style={{ all: 'unset' }}
ref={ref}
({ onRender, ...props }: StoryProps, ref: React.Ref<HTMLDivElement>) => {
useEffect(() => onRender && onRender());
return (
<StoryBlockContainer {...props}>
{(context, rest) => {
const { story, options = {} } = context;
if (story && story.renderFn) {
try {
const values = getControlValues(story.controls);
const { decorators: globalDecorators = [] } = options;
const { decorators: storyDecorators = [] } = story;
const decorators = deepMerge(globalDecorators, storyDecorators);
const renderFn = decorators.reduce(
(acc: StoryRenderFn, item: StoryRenderFn) => () =>
item(acc, { context }),
//@ts-ignore
() => story.renderFn(values, { context }),
);
return (
<Box
id={story.id}
sx={{
px: 4,
py: 3,
}}
{...rest}
>
{createElement(renderFn)}
</div>
</Box>
);
} catch (e) {
console.error(e);
<div
className="story-render-container"
style={{ all: 'unset' }}
ref={ref}
>
{createElement(renderFn)}
</div>
</Box>
);
} catch (e) {
console.error(e);
}
}
}
console.error('Story function not found', props, story);
return null;
}}
</StoryBlockContainer>
),
console.error('Story function not found', props, story);
return null;
}}
</StoryBlockContainer>
);
},
);

Story.displayName = 'Story';
3 changes: 2 additions & 1 deletion ui/blocks/src/context/block/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class ErrorBoundary extends Component {
const { error } = this.state;

if (error) {
return <h1>Error caught.</h1>;
console.log(error);
return <h1>Unexpected error.</h1>;
}

return children;
Expand Down
12 changes: 6 additions & 6 deletions ui/blocks/src/context/components/ComponentsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ export const useComponentsContext = ({
const [{ story, kind, component, componentPackage }, setStoryData] = useState(
getStoryData(storyId),
);

if (!story) {
return {
components: {},
};
}
useEffect(() => {
setStoryData(getStoryData(storyId));
const onChange = () => {
Expand All @@ -56,6 +50,12 @@ export const useComponentsContext = ({
removeObserver(onChange);
};
}, [storyId]);

if (!story) {
return {
components: {},
};
}
let components: StoryComponents = {};
if (of === CURRENT_STORY) {
if (component) {
Expand Down

0 comments on commit 9269694

Please sign in to comment.