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

Storybook show code fix #117

Merged
merged 28 commits into from Apr 24, 2021
Merged

Storybook show code fix #117

merged 28 commits into from Apr 24, 2021

Conversation

ogjtech
Copy link
Contributor

@ogjtech ogjtech commented Mar 25, 2021

Close #15.
Adding the following code:

docs: {
    source: {
        type: "code"
    }
}

To the export default {...} as Meta; in each Storybook file shows the component code as they are defined in the file.
For example, the Box component now shows:

(args: any) => (
  <Box {...args}>
    <p>This is a Box component.</p>
  </Box>
)

In the Docs tab.

@ogjtech ogjtech self-assigned this Mar 25, 2021
@ogjtech ogjtech linked an issue Mar 25, 2021 that may be closed by this pull request
3 tasks
@DanielvanVliet
Copy link
Contributor

DanielvanVliet commented Mar 27, 2021

I'm not sure if this is the right approach. Although the displayed code does work now, all information on the arguments for each variant is lost. See button for instance, all variants show the same code now.

Perhaps its best to inspect each component and determine whether
type: 'code' or type: 'dynamic'
Leads to better results.

@bddjong
Copy link
Contributor

bddjong commented Mar 27, 2021

I'm not sure if this is the right approach. Although the displayed code does work now, all information on the arguments for each variant is lost. See button for instance, all variants show the same code now.

Agreed. This is going in the right direction, but we should have working code examples for all story templates. Take a look at the NLDS POC for example: http://storybook.nldesignsystem.nl/?path=/docs/components--backlink

@bddjong bddjong self-requested a review March 27, 2021 09:53
Copy link
Contributor

@joostvanviegen joostvanviegen left a comment

Choose a reason for hiding this comment

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

I agree with the comments from @bddjong and @DanielvanVliet and won't be giving an explicit appproval for now.

@DanielvanVliet
Copy link
Contributor

It turns out Storybook has three options for the source code in 'show code'. (storybook docs)

  1. Dynamic with type: dynamic option, which is the default
  2. Render the source code of the story as a whole with type: code
  3. Pass in a custom string to be rendered in the show code.

Stories which require no setup code, such as Button, should generate usable code in the docs with the default type: 'dynamic' option:

reasonable

The more complicated stories such as DatePicker require some setup code (e.g. a React.useState) to function properly.

The dynamic option does not result in usable code in this case, as all setup code is removed:
bad

Using the second option (rendering the storybook source code) for these more complicated stories is better:
better
However this code does not respond to changes in the properties, and is thus the same for each variant in the story.
And more importantly, is still missing imports such as DateFnsUtils and might not work.

We can also supply storybook with our own string that should be shown in the 'show code'.
This may be different for each variant, but is still static and will not respond to changes in the properties on the docs page.
This also means that we have to ensure that the code works and this can add alot of work and maintenance when API changes are made.

Still providing custom examples is probably the best way forward for the more 'complicated' components, if we always want to display usable code.
Any thoughts? @Gemeente-DenHaag/mdh-rysst @mjcarsjens

@joostvanviegen
Copy link
Contributor

It turns out Storybook has three options for the source code in 'show code'. (storybook docs)

1. Dynamic with `type: dynamic` option, which is the default

2. Render the source code of the story as a whole with `type: code`

3. Pass in a custom string to be rendered in the show code.

Stories which require no setup code, such as Button, should generate usable code in the docs with the default type: 'dynamic' option:

reasonable

The more complicated stories such as DatePicker require some setup code (e.g. a React.useState) to function properly.

The dynamic option does not result in usable code in this case, as all setup code is removed:
bad

Using the second option (rendering the storybook source code) for these more complicated stories is better:
better
However this code does not respond to changes in the properties, and is thus the same for each variant in the story.
And more importantly, is still missing imports such as DateFnsUtils and might not work.

We can also supply storybook with our own string that should be shown in the 'show code'.
This may be different for each variant, but is still static and will not respond to changes in the properties on the docs page.
This also means that we have to ensure that the code works and this can add alot of work and maintenance when API changes are made.

Still providing custom examples is probably the best way forward for the more 'complicated' components, if we always want to display usable code.
Any thoughts? @Gemeente-DenHaag/mdh-rysst @mjcarsjens

I think we should go with a hybrid between 'dynamic', 'code' and 'custom'. Whenever a story is simple enough to use dynamic we'll do so, for the other ones we should decide on a case by case basis if 'code' or 'custom' is better.

@mjcarsjens
Copy link
Contributor

mjcarsjens commented Apr 2, 2021

I personally feel like we should try to use dynamic where possible. In cases where this does not work, for example the DatePicker component, my personal preference would go out to writing a custom code block, as follows:

Inline.parameters = {
  docs: {
    source: {
      code:
`
import DateFnsUtils from "@date-io/date-fns";

<PickersUtilsProvider utils={DateFnsUtils}>
  <DatePicker variant="inline" />
</PickersUtilsProvider>
`
    }
  }
}

This does bring along some more maintenance but these are a few rare cases from what I can find, and when API changes are made you would have to update the Story file anyway, so updating 3 code examples for the variants of the component doesn't seem like that much extra work.

I would try to stay away from type: "code" as this does not correctly display how to actually get the variant which is shown.

With regards to components like the Accordion and the AppBar: There seems to be a problem with the @material-ui/icons components in combination with StoryBook, as these all show up as <[Object object] />. I would not put too much effort into fixing this, if the problem still persists once we implemented our own icons this is something we can look at, for now I would simply disregard these issues.

@ogjtech
Copy link
Contributor Author

ogjtech commented Apr 3, 2021

@Gemeente-DenHaag/mdh-rysst @mjcarsjens
The Badge component is one such example of showing [object Object] when the code is displayed in the dynamic variant.
To fix this, I have replaced the type of the args parameter to BadgeProps, in order to show the Badge as it is defined in the code and to show more specifically what props can be supplied. Badge is now defined as follows:

const Template: Story<BadgeProps> = (args: BadgeProps) => (
  <Badge {...args}>
    <MailIcon />
  </Badge>
);

…nent to properly display its implementation.
@ogjtech
Copy link
Contributor Author

ogjtech commented Apr 3, 2021

In cases where this does not work, for example the DatePicker component, my personal preference would go out to writing a custom code block

@mjcarsjens Even though this does show the component as is, it can only be defined once in a Storybook file, not per defined variant. It is not technically impossible, but it would require us to rewrite the way we structure our Storybook files and resort to an outdated way of defining the Templates that are shown. This would mean we need to rewrite all of the storybook files, which in my personal opinion is more trouble than its worth.

Personally I think it better to, for now, just accept that the code is not shown as ideal and trust on the implementer's ability to read documentation and interpret code.

@mjcarsjens
Copy link
Contributor

In cases where this does not work, for example the DatePicker component, my personal preference would go out to writing a custom code block

@mjcarsjens Even though this does show the component as is, it can only be defined once in a Storybook file, not per defined variant. It is not technically impossible, but it would require us to rewrite the way we structure our Storybook files and resort to an outdated way of defining the Templates that are shown. This would mean we need to rewrite all of the storybook files, which in my personal opinion is more trouble than its worth.

Personally I think it better to, for now, just accept that the code is not shown as ideal and trust on the implementer's ability to read documentation and interpret code.

@Gemeente-DenHaag/mdh-rysst @ogjtech
Not exactly true though, you can override the default parameters for each variant:

The code:
image

The result:
image

As you can see defining a code template for an individual variant does only change the Show Code feature for that specific variant and it is very much possible to define a own code definition for each variant (see my experimental commit to take a look at the exact code).

I'm not saying your conclusion is wrong in any way, I think this is a decision that should be made with the team as a whole and I'm not trying to veto what we should do. All I'm trying to say is that the option to provide a custom code template for every variant is still possible and can be considered as an option.

@ogjtech
Copy link
Contributor Author

ogjtech commented Apr 10, 2021

@mjcarsjens I have tried your approach and, while it does work, it shows very weird indentation. I have tried implementing some of the fixes suggested in this issue, but none of them seem to work.
This issue has existed since September 2019, but to this day has not seen a fix of any kind.

Copy link
Contributor

@DanielvanVliet DanielvanVliet left a comment

Choose a reason for hiding this comment

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

Currently there are additional spaces inserted in the code examples.
One way to avoid this would be creating the code variables outside the story config.

Can you do this for all the stories?

src/stories/datadisplay/Divider.stories.tsx Outdated Show resolved Hide resolved
MDH RYSST Sprint 11 automation moved this from In progress to Review in progress Apr 24, 2021
MDH RYSST Sprint 11 automation moved this from Review in progress to Waiting for merge to master Apr 24, 2021
@GewoonMaarten GewoonMaarten merged commit c82c289 into development Apr 24, 2021
MDH RYSST Sprint 11 automation moved this from Waiting for merge to master to Done Apr 24, 2021
@GewoonMaarten GewoonMaarten deleted the storybook_show_code_fix branch April 24, 2021 12:08
@Robbert
Copy link
Member

Robbert commented Apr 24, 2021

@ogjtech If that multiline string indentation is such an annoying recurring issue, you could consider using this: https://www.npmjs.com/package/ts-dedent

@Robbert
Copy link
Member

Robbert commented Apr 24, 2021

Also: wouldn't it be convenient to have prettier --check in the CI/CD pipeline? If someone doesn't have the Prettier format-on-save installed it becomes immediately apparent, and you don't have to dweil with de crane open.

@GewoonMaarten GewoonMaarten mentioned this pull request Apr 28, 2021
joostvanviegen pushed a commit that referenced this pull request May 8, 2021
* Show code showing actual code in layout Storybook.

* Show code showing actual code in navigation Storybook.

* Show code showing actual code in surfaces Storybook.

* Show code showing actual code in data display Storybook.

* Show code showing actual code in input Storybook.

* Experimental commit, providing different types of code docs per component to properly display its implementation.

* Redefined code examples of components based on best possible overview

* Experimental commit: Show possibility of providing custom code documentation for each component variant in Storybook

* Experimental commit: custom code docs per component.

* Applied per-component docs specification for input components.

* Applied per-component code doc specs to layout components.

* Applied per-component code doc specs to navigation components.

* Applied per-component code doc specs to surface components.

* Shortened import

* Defined custom code in separate variable to fix indentation

* Defined custom code in separate variable to fix indentation for input components.

* Defined custom code in separate variable to fix indentation for navigation components.

* Defined custom code in separate variable to fix indentation for navigation (yes, twice) components.

* Defined custom code in separate variable to fix indentation for surface components.

* Fixed indendation for surfaces.. Again...

* Fixed indendation for navigation.. Again...

* Fixed indendation for layout.. Again...

* Fixed indendation for input.. Again...

* Fixed indendation for datadisplay.. Again...

* Prettifier go brrr

* Prettifier go brrr and also fixed indentation again because Checkboxes decided to be selfish pricks.

Co-authored-by: Maarten Carsjens <maarten.carsjens@gxsoftware.com>
@DanielvanVliet DanielvanVliet mentioned this pull request May 8, 2021
DanielvanVliet added a commit that referenced this pull request Jun 19, 2021
* Only allow lerna publish from master

* Publish

 - @gemeente-denhaag/accordionactions@0.1.7
 - @gemeente-denhaag/accordiondetails@0.1.7
 - @gemeente-denhaag/accordionsummary@0.1.7
 - @gemeente-denhaag/accordion@0.1.7
 - @gemeente-denhaag/appbar@0.1.7
 - @gemeente-denhaag/avatargroup@0.1.7
 - @gemeente-denhaag/avatar@0.1.7
 - @gemeente-denhaag/badge@0.1.7
 - @gemeente-denhaag/basedatadisplayprops@0.1.7
 - @gemeente-denhaag/baselayoutprops@0.1.7
 - @gemeente-denhaag/baseprops@0.1.7
 - @gemeente-denhaag/box@0.1.7
 - @gemeente-denhaag/buttongroup@0.1.7
 - @gemeente-denhaag/button@0.1.7
 - @gemeente-denhaag/cardactions@0.1.7
 - @gemeente-denhaag/cardcontent@0.1.7
 - @gemeente-denhaag/cardheader@0.1.7
 - @gemeente-denhaag/card@0.1.7
 - @gemeente-denhaag/checkbox@0.1.7
 - @gemeente-denhaag/container@0.1.7
 - @gemeente-denhaag/divider@0.1.7
 - @gemeente-denhaag/drawer@0.1.7
 - @gemeente-denhaag/formcontrollabel@0.1.7
 - @gemeente-denhaag/formcontrol@0.1.7
 - @gemeente-denhaag/formgroup@0.1.7
 - @gemeente-denhaag/gridlisttilebar@0.1.7
 - @gemeente-denhaag/gridlisttile@0.1.7
 - @gemeente-denhaag/gridlist@0.1.7
 - @gemeente-denhaag/grid@0.1.7
 - @gemeente-denhaag/hidden@0.1.7
 - @gemeente-denhaag/iconbutton@0.1.7
 - @gemeente-denhaag/inputlabel@0.1.7
 - @gemeente-denhaag/link@0.0.2
 - @gemeente-denhaag/listitemavatar@0.1.7
 - @gemeente-denhaag/listitemicon@0.1.7
 - @gemeente-denhaag/listitemsecondaryaction@0.1.7
 - @gemeente-denhaag/listitemtext@0.1.7
 - @gemeente-denhaag/listitem@0.1.7
 - @gemeente-denhaag/listsubheader@0.1.7
 - @gemeente-denhaag/list@0.1.7
 - @gemeente-denhaag/menuitem@0.1.7
 - @gemeente-denhaag/menulist@0.1.7
 - @gemeente-denhaag/menu@0.1.7
 - @gemeente-denhaag/mobilestepper@0.1.7
 - @gemeente-denhaag/paper@0.1.7
 - @gemeente-denhaag/pickersutilsprovider@0.1.10
 - @gemeente-denhaag/pickers@0.1.14
 - @gemeente-denhaag/popover@0.1.7
 - @gemeente-denhaag/popper@0.1.7
 - @gemeente-denhaag/radiogroup@0.1.7
 - @gemeente-denhaag/radio@0.1.7
 - @gemeente-denhaag/select@0.1.7
 - @gemeente-denhaag/stepbutton@0.1.7
 - @gemeente-denhaag/stepconnector@0.1.7
 - @gemeente-denhaag/stepcontent@0.1.7
 - @gemeente-denhaag/stepicon@0.1.7
 - @gemeente-denhaag/steplabel@0.1.7
 - @gemeente-denhaag/step@0.1.7
 - @gemeente-denhaag/stepper@0.1.7
 - @gemeente-denhaag/swipeabledrawer@0.1.7
 - @gemeente-denhaag/switch@0.1.7
 - @gemeente-denhaag/tabcontext@0.1.7
 - @gemeente-denhaag/tablist@0.1.7
 - @gemeente-denhaag/tabpanel@0.1.7
 - @gemeente-denhaag/tabscrollbutton@0.1.7
 - @gemeente-denhaag/tab@0.1.7
 - @gemeente-denhaag/tabs@0.1.7
 - @gemeente-denhaag/textfield@0.1.7
 - @gemeente-denhaag/toolbar@0.1.7
 - @gemeente-denhaag/typography@0.1.7
 - @gemeente-denhaag/datadisplay@0.1.5
 - @gemeente-denhaag/denhaag-component-library@0.1.14
 - @gemeente-denhaag/input@0.1.12
 - @gemeente-denhaag/layout@0.1.5
 - @gemeente-denhaag/navigation@0.1.5
 - @gemeente-denhaag/surfaces@0.1.5

* Added initial version of card and cardheader styling

* Applied styling to Card and changed card's hex values to hsl

* switching cardstyle based on the variant.

* Added color to card case and padding to the top of card

* WIP:Including multiple components in Card
Including multiple components in card to adhere to the designs.

* Removed padding from cardcontent

* Moved cards story to reflect the way cards are represented in the designs

* Fixed arrow position and text color

* Fixed border of Case / card
Added font weight to title and subtitle
Sorted css file by property / design token names

* Added paper svg file to case / card

* Added title, subtitle and date properties to card

* Correctly padded date on left side

* Correctly positioned the date and arrow on cards

Co-authored-by: Daniel van Vliet <Danielvvliet@hotmail.com>

* Suggested changes to Card to show properly as folder and changed around class names to be conform BEM standards

* Added correct borders to card/case

Co-authored-by: Daniel van Vliet <Danielvvliet@hotmail.com>

* Added on-hover perspective change to card/case

* Remove merge conflicts and reduce amount of design tokens for Button

* Made text darker on hover

* WIP: focus border color title and subtitle

* Add base styles package

* Add css as default export

* Change tsx in basestyles/package.json to ts

* Changed colour hex values to hsl
(Hue Saturation Luminesence)

* Fix incorrect HSL value for --denhaag-green-1

* Changed hsl values to modern notation
also reverted change in stylelint config to change color notation to modern.

* Update README.md

* Storybook show code fix (#117)

* Show code showing actual code in layout Storybook.

* Show code showing actual code in navigation Storybook.

* Show code showing actual code in surfaces Storybook.

* Show code showing actual code in data display Storybook.

* Show code showing actual code in input Storybook.

* Experimental commit, providing different types of code docs per component to properly display its implementation.

* Redefined code examples of components based on best possible overview

* Experimental commit: Show possibility of providing custom code documentation for each component variant in Storybook

* Experimental commit: custom code docs per component.

* Applied per-component docs specification for input components.

* Applied per-component code doc specs to layout components.

* Applied per-component code doc specs to navigation components.

* Applied per-component code doc specs to surface components.

* Shortened import

* Defined custom code in separate variable to fix indentation

* Defined custom code in separate variable to fix indentation for input components.

* Defined custom code in separate variable to fix indentation for navigation components.

* Defined custom code in separate variable to fix indentation for navigation (yes, twice) components.

* Defined custom code in separate variable to fix indentation for surface components.

* Fixed indendation for surfaces.. Again...

* Fixed indendation for navigation.. Again...

* Fixed indendation for layout.. Again...

* Fixed indendation for input.. Again...

* Fixed indendation for datadisplay.. Again...

* Prettifier go brrr

* Prettifier go brrr and also fixed indentation again because Checkboxes decided to be selfish pricks.

Co-authored-by: Maarten Carsjens <maarten.carsjens@gxsoftware.com>

* Fix/#42 fix installation files (#137)

* add script to bulk update package.json files

* Renamed and moved source files

Renamed all files to index.tsx and moved into src directory. This is to be inline with conventions.

* Update fields in package.json

* fix bug in update script

* sorted package.json files

* fix build

* Only allow lerna publish from master

* add dependency to steplabel

* updated package.json jsx flag

* add production key to microbundle

* added peerdependency on react, fixed imports

* picker potential fix

* pickers index cleanup

* pickerutilsprovider fix

* removed duplicate export of defaults

* use tsc instead of microbundler

* Merge branch 'fix/#42-fix-installation-files' of https://github.com/Gemeente-DenHaag/denhaag-component-library into fix/#42-fix-installation-files

* fix dependencies and add npmignore

* remove files field from package.json

* Fixing imports in storybook stories

* Moved stories to component directory

Co-authored-by: Maarten <GewoonMaarten@users.noreply.github.com>

* remove old paths

* update files field in package.json

* add build config to meta-packages

* remove module field from tsconfig

* update clean script

* remove deprecated script

* fixing compatibility of yarn clean for windows & linux

* update tsconfig path and remove npmignore

* update clean script

* update steplabel command

* upgraded dependencies of react and storybook, fixed stories

* merge fix

* update react and storybook

* upgraded storybook dependencies

* yarn lock update

* fix textfield import

* remove src from imports

* add doc for install packages locally to contributing.md

* remove trailing comma

* make incremental build actually work

* remove commented code

Co-authored-by: Maarten Carsjens <mjcarsjens@gmail.com>
Co-authored-by: bdjong <bryandejong99@gmail.com>
Co-authored-by: Maarten Carsjens <maarten.carsjens@gxsoftware.com>
Co-authored-by: DanielvanVliet <danielvvliet@hotmail.com>

* Hot fix (#149)

* fix incorrect git merge in IconButton

* add compiler options for root tsconfig.json

Appearantly this tsconfig.json is used by storybook. It doesn't show the controls if the compiler options are not included

* update paths for linters

* make husky scripts executable

* Eslint storybook warning fixes

* More eslint warnings in storybook files fixed

Co-authored-by: DanielvanVliet <danielvvliet@hotmail.com>

* Feature/#46 icons (#158)

* add boilerplate project

* add the ability for storybook to include svg files

* wrap svgIcon

* move svg icon file

* add default story

* add type support for svg files

* add all svg icons from figma

* update svg type

* wrap all icons

* forward props

* display all icons in storybook

* replace all fill and stroke values with "currentColor"

* fix icon fill

* update shapeRendering to be enum

* add single icon story

* install svgo and add to build script

optimize all the svgs!

* Update README with contributing info

* update show code for single icon

* replace material icons with our own in storybook stories

* replace x with *

* remove @material-ui/icons as dependency

* Move basestyles to styles subfolder

* Split design tokens and brand tokens

* Add styles folder to workspaces

* Use den haag brand tokens in storybook

* Fixed visibility of focus border

* Made card focussable

* merge changes

* Removed include of card story in tsconfig cardactions

* Add css files to dist directories of Card, CardHeader, CardContent

* Fixed style of card/case on focus

* Fixed story book args to map to interface

* Removed unused raised property from card

* Moved card /case design tokens into Basestyles Package

* Use builtin fontsizes

* Fixed some design token references in Card/Case

* Increased height on background::before to remove gap

* Compensate padding of border text with focus

* Added workaround because of chrome focus state

* Changed font size token to point to correct css variable

* Updated the Card because of new styling packages

* Move css to .css files instead of  .module.css to fix clean react project issues

* change css variable names to adhere correct naming scheme

* Changed naming of some css variables

* Changed focusborder of text to outline and added outline to arrow of
Card/Case

* Added onclick and tabindex as properties

* NewLines

* Fix spelling errors secundary -> secondary

* Remove commented code

* Simplified border radiu of card--case

* Remove obsolete tokens files

* Use React event for onclick

* Fixed bem classnames

* Added time html5 tag around the date in card

* Fixed the input of time html5 tag

* Renamed CardActions file and reduced exports of Card

* Fix MouseEvent type

* Move some remaiing tokens to tokens-card

* Sorted all properties alphabetically

* card-header bem naming

* Remove cardcasefocusclasses from bem mapping

* Rename card__text wrapper to card-text-wrapper

* Move cardactions styling token to tokens-card

* Cleanup properties of actions and content

* Removed obsolete cardheader component

* added --hover and --focus to selectors and formatted the selectors better

* Correctly fix bem naming of the text wrapper

* Fixed some inconsistencies in card.css

* Added stylelint ignore line descending specifity
In order to ignore descending specifity for one line which would
otherwise result in duplicated css just for testing purposes.

* Clearer naming of wrapper padding

* Change override of cardcontent: use start and end

* Added focus border around all arrow card variants

* split title padding tokens

* removed tabindex from card/case and changed the title to an anchor tag. Fixed focus styles to work with these changes.

* replaced padding inline and block properties by their start and end counterparts

Co-authored-by: Daniel van Vliet <Danielvvliet@hotmail.com>

* Ran prettier to fix some minor issues

* Fixed CSS token naming

* removed typography component

Co-authored-by: Maarten Carsjens <mjcarsjens@gmail.com>
Co-authored-by: Maarten <GewoonMaarten@users.noreply.github.com>
Co-authored-by: bdjong <bryandejong99@gmail.com>
Co-authored-by: Maarten Carsjens <maarten.carsjens@gxsoftware.com>
Co-authored-by: Daniel van Vliet <Danielvvliet@hotmail.com>
Co-authored-by: Bryan de Jong <bryan.dejong@denhaag.nl>
Co-authored-by: Jeroen de Klerk <jtech.work@protonmail.com>
@bddjong bddjong removed this from Released in MDH RYSST Sprint 11 Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Storybook's 'show code' option displays incorrect code
8 participants