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

Fix storybook backgrounds in MDX #1925

Merged
merged 2 commits into from
Mar 12, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Fix sb mdx backgrounds",
"packageName": "@ni/nimble-components",
"email": "[email protected]",
"dependentChangeType": "none"
}
27 changes: 27 additions & 0 deletions packages/nimble-components/.storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@ export const parameters = {
}
};

const createOrUpdateBackgroundWorkaround = style => {
let styleElement = document.querySelector('.nimble-background-workaround');
if (!styleElement) {
styleElement = document.createElement('style');
styleElement.classList.add('nimble-background-workaround');
document.head.append(styleElement);
}
styleElement.innerHTML = style;
};

// Storybook background plugin does not support mdx
// Workaround based on: https://github.com/storybookjs/storybook/issues/13323#issuecomment-876296801
export default {
decorators: [
(story, context) => {
const defaultBackgroundColorKey = context?.parameters?.backgrounds?.default;
const defaultBackgroundColor = context?.parameters?.backgrounds?.values?.find(v => v.name === defaultBackgroundColorKey)?.value;
const currentBackgroundColor = context?.globals?.backgrounds?.value ?? defaultBackgroundColor;
const style = `.docs-story {
background-color: ${currentBackgroundColor}
}`;
createOrUpdateBackgroundWorkaround(style);
return story();
}
]
};

// Storybook's default serialization of events includes the serialized event target. This can
// be quite large, such as in a table with a lot of records. Therefore, the serialization depth
// should be limited to avoid poor performance.
Expand Down
Loading