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
POC: Extract Markdown links and headers as components. #613 #619
POC: Extract Markdown links and headers as components. #613 #619
Conversation
…n be overridden in styleguide.config.js. styleguidist#613
Re: Issue #613 |
Hi @sapegin This is a WIP and more of a proof of concept right now. I wanted to go ahead and get a PR out there so that I can get some feedback before continuing further down this path. My strategy is to break up the large I've overridden links and headers in this manner in the customised example. This seems to be consistent with the patterns already established, flexible, allow custom theming and styles to still work, and does not introduce much extra complexity. Let me know what you think! |
Hey @ryanoglesby08, thank you very much for your pull request! I’m just back from React Alicante (it was amazing!) and will have a look soon ;-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the general idea of breaking the UI into small peaces, but I think the implementation is more complex than it could be:
We already have a Link component so you can reuse it directly. I’d extract Header component the same way so it could be reused not only in Markdown. And I’m not sure we need to store these configs in separate files.
Re: I’d extract Header component the same way so it could be reused not only in Markdown. Sure. I can do that eventually. Re: And I’m not sure we need to store these configs in separate files. By "configs" I assume you mean the mappings of component + props to the corresponding tag name that gets translated by markdown-to-jsx. I think storing the configs in separate files enables modularity and creates a more flexible solution. By storing the configs in separate files, it allows the react component library to define its own mappings per component without having to override a giant Markdown.js file. Overriding any Markdown element with a new component requires 2 things: 1) the new component and 2) a new mapping of the Markdown element to the component and props. If I did not keep the configs separate, it means more files need to change to override a single Markdown element. Do you have other ideas for how I might approach this? |
Do you see many use cases when people would want to define separate components for each heading level? Do you think it’s important to differentiate components (link, header, etc.) that are used in Markdown and the same components that are used in Styleguidist UI? I think using normal component (when you export a real component) increases modularity and much less surprising.
In both cases you would need to override one file: file that exports component or file that exports mapping and hides component inside. |
Hi @sapegin. I've finally been able to work a bit more on this. I've changed the approach a little bit based on your feedback. This feels like a good approach to me. Here is the structure I've come out with so far. Let me know of your feedback. rsg-components examples/customised src/components styleguide/components styleguide.config.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it much more, thanks!
} | ||
|
||
export const styles = ({ color, fontSize, fontFamily }) => ({ | ||
heading: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d compose using cx() function — would make code simpler and shorter.
|
||
import Styled from 'rsg-components/Styled'; | ||
|
||
import HeadingRenderer from 'rsg-components/Heading/HeadingRenderer'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be import HeadingRenderer from 'rsg-components/Heading'
— *Render
components are implementation detail, we should’t leak to the caller code. See for details: https://react-styleguidist.js.org/docs/development.html
import cx from 'classnames'; | ||
import Styled from 'rsg-components/Styled'; | ||
|
||
function HeadingRenderer({ classes, depth, id, deprecated, children }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
level
sounds better than depth
for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure can use level
. I used depth
because that prop name was already in place from the SectionHeadingRenderer
, which is where this component was extracted from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see ;-) This line is funny:
const headingLevel = Math.min(6, depth);
Let’s make it level
everywhere.
|
||
function MarkdownHeading({ classes, depth, children }) { | ||
return ( | ||
<div className={classes.spacing}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe Heading
component should accept className
? And we could get rid of the div here.
<div className={classes.toolbar}>{toolbar}</div> | ||
</Tag> | ||
<HeadingRenderer id={id} depth={depth} deprecated={deprecated}> | ||
<div className={classes.root}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And get rid or this div as well ;-)
Thanks for the feedback, will work on incorporating your suggestions. I will also continue down this path and implement things more cleanly and tested, with the end goal of replacing all the Markdown overrides with replaceable components: To do:
|
Also, I noticed that the Markdown.js file has styles for "input" elements. Is this necessary? There is no Markdown syntax for inputs, and I'm not sure the purpose of trying to embed inputs in Markdown documentation. |
Maybe better to split into two (or more PRs) if that’s convenient for you.
They are for todos like in your previous comment ;-) |
Cool, will break it up into multiple PRs. Will leave this one here to keep the todo list if you don't mind. |
I’d finish this one because it looks very close to done and then continue when you have time. |
@ryanoglesby08 actually GFM task lists will render inputs: https://github.com/blog/1375-task-lists-in-gfm-issues-pulls-comments GFM is supported in markdown-to-jsx |
All elements within Markdown now have associated components, making them easy to override consumers. Closing this PR. Thanks for all the suggestions and help @sapegin |
... so that they can be overridden in styleguide.config.js.