Skip to content

Commit 78a7ecf

Browse files
authored
fix(web-components): storybook code display formatting (#32921)
1 parent 3843ed3 commit 78a7ecf

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

packages/web-components/.storybook/preview.mjs

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { teamsDarkTheme, teamsLightTheme, webDarkTheme, webLightTheme } from '@fluentui/tokens';
2-
import webcomponentsTheme from './theme.mjs';
2+
import * as prettier from 'prettier';
3+
import prettierPluginHTML from 'prettier/parser-html.js';
34
import { setTheme } from '../src/theme/set-theme.js';
5+
import webcomponentsTheme from './theme.mjs';
46

57
import '../src/index-rollup.js';
68
import './docs-root.css';
79

10+
const FAST_EXPRESSION_COMMENTS = /<!--((fast-\w+)\{.*\}\2)?-->/g; // Matches comments that contain FAST expressions
11+
812
const themes = {
913
'web-light': webLightTheme,
1014
'web-dark': webDarkTheme,
@@ -36,6 +40,31 @@ export const parameters = {
3640
},
3741
},
3842
docs: {
43+
source: {
44+
// To get around the inability to change Prettier options in the source addon, this transform function
45+
// imports the standalone Prettier and uses it to format the source with the desired options.
46+
transform(/** @type {string} */ src, /** @type {import('@storybook/html').StoryContext} */ storyContext) {
47+
if (!src) {
48+
const fragment = storyContext.originalStoryFn(storyContext.allArgs, storyContext);
49+
if (!(fragment instanceof DocumentFragment) && !(fragment instanceof HTMLElement)) {
50+
return;
51+
}
52+
53+
const div = document.createElement('div');
54+
div.append(fragment);
55+
src = div.innerHTML;
56+
}
57+
58+
src = src.replace(FAST_EXPRESSION_COMMENTS, ''); // remove comments
59+
src = src.replace(/=""/g, ''); // remove values for boolean attributes
60+
src = prettier.format(src, {
61+
htmlWhitespaceSensitivity: 'ignore',
62+
parser: 'html',
63+
plugins: [prettierPluginHTML],
64+
});
65+
return src;
66+
},
67+
},
3968
theme: webcomponentsTheme, // override the default Storybook theme with a custom fluent theme
4069
},
4170
};

packages/web-components/src/accordion-item/accordion-item.stories.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,18 @@ export default {
9999
},
100100
} as Meta<FluentAccordionItem>;
101101

102-
export const Default: Story = {};
103-
104-
export const AccordionItem: Story = {
102+
export const Default: Story = {
105103
args: {
106104
expanded: true,
107105
headinglevel: 2,
108106
headingSlottedContent: () => html`<span slot="heading">Description</span>`,
109107
slottedContent: () => html`
110108
<fluent-text>
111109
<p>
112-
Chalk is a soft, white, porous, sedimentary carbonate rock, a form of limestone composed of the mineral
113-
calcite. Calcite is an ionic salt called calcium carbonate or CaCO3. It forms under reasonably deep marine
114-
conditions from the gradual accumulation of minute calcite shells shed from micro-organisms called
115-
coccolithophores. Flint is very common as bands parallel to the bedding or as nodules embedded in chalk.
110+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras lacinia bibendum metus, commodo dapibus erat.
111+
Aliquam venenatis gravida malesuada. Maecenas ut porttitor justo, sed euismod ex. Suspendisse sodales enim
112+
sem, in auctor risus aliquam ac. Cras ut velit lacinia diam varius fermentum. Sed sed augue tempus, rhoncus
113+
neque vestibulum, placerat risus.
116114
</p>
117115
</fluent-text>
118116
`,

0 commit comments

Comments
 (0)