Skip to content

Commit

Permalink
fix: handle of="." in MDX
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jul 9, 2020
1 parent aaab6c3 commit 98cbb2d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
7 changes: 6 additions & 1 deletion core/instrument/src/babel/mdx-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export const extractMDXStories = (props: any) => (
const collectComponent = (attributes: any) => {
const attrComponents = componentsFromParams(attributes);
components = attrComponents.reduce(
(acc, componentName) => ({ ...acc, [componentName]: undefined }),
(acc, componentName) => ({
...acc,
[componentName === '.' && store.doc
? (store.doc.component as string)
: componentName]: undefined,
}),
components,
);
return attrComponents.length > 0 ? attrComponents[0] : undefined;
Expand Down
26 changes: 15 additions & 11 deletions ui/blocks/src/context/block/BlockDataContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,25 @@ export const BlockDataContextProvider: React.FC<BlockDataContextInoutProps> = ({
const getComponents = (
components: { [key: string]: any } | undefined,
doc: Document | undefined,
): StoryComponents =>
store && doc && components
): StoryComponents => {
return store && doc && components
? Object.keys(components).reduce((acc, key) => {
const name = getComponentName(components[key]);
const component =
name &&
doc?.componentsLookup[name] &&
store?.components[doc.componentsLookup[name]];
if (component) {
return { ...acc, [key]: component };
} else {
return acc;
const comp = components[key];
const name = getComponentName(
comp === CURRENT_STORY ? doc.component : comp,
);
if (name) {
const component =
doc?.componentsLookup[name] &&
store?.components[doc.componentsLookup[name]];
if (component) {
return { ...acc, [key]: component };
}
}
return acc;
}, {})
: {};
};

const storyIdFromName = (name: string): string | undefined => {
if (store) {
Expand Down
8 changes: 1 addition & 7 deletions ui/blocks/src/context/components/ComponentsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,8 @@ export const useComponentsContext = ({
removeObserver(onChange);
};
}, [docId, storyId, addObserver, getStoryData, removeObserver]);

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

0 comments on commit 98cbb2d

Please sign in to comment.