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

DevDocs: Generate a table for displaying proptypes #10342

Closed
wants to merge 22 commits into from

Conversation

withinboredom
Copy link
Contributor

@withinboredom withinboredom commented Dec 30, 2016

Generates a table of proptypes when viewing a component by itself in the DevDocs.

like

#9919 added a json file to the build that uses react/react-docgen to parse our components and extract the propTypes for each of them. This happens at build time. A later PR may allow this to run similar to how our CSS files are built while developing.

This PR requires that file directly, and doesn't require a server. This has two benefits:

  1. It keeps the code simple
  2. It doesn't require a running server (offline mode -- as seen above)

A different PR can move the file somewhere in the client path. I'm not particularly happy about a relative path in this code. However, I feel as though that should be separate and not done in this PR.

The styling of this follows @aduth's state selector PR (#9433) style. I've tagged this for design review. I originally experimented with using flex-box instead of a table, but the complexity of the dom and CSS exploded on me. It would be a good future task, though I imagine most developers would be on a desktop device writing code. I could be wrong 😉 ...

There are some interesting possibilities that could come in a future PR near you:

This work is based on #9289, #9919. There's a fix in #10343 to get some missing components into the index.


View live: https://calypso.live/devdocs/blocks?branch=add/proptypes-table

@withinboredom withinboredom self-assigned this Dec 30, 2016
@withinboredom withinboredom requested a review from ehg January 3, 2017 20:26
@withinboredom withinboredom added [Status] Needs Design Review Add this when you'd like to get a review / feedback from the Design team on your PR [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] In Progress labels Jan 3, 2017
@folletto
Copy link
Contributor

folletto commented Jan 4, 2017

I think design wise it's good for this iteration. I agree it might require tweaking, but it's already good enough, and we can iterate on that once it's in.

The only frustrating bit is that going back to "All Blocks" loses the scroll position in the list, which makes navigating a few blocks fairly frustrating. Might not be something to be done in this PR specifically, but I think it's important to address that.

@folletto folletto removed the [Status] Needs Design Review Add this when you'd like to get a review / feedback from the Design team on your PR label Jan 4, 2017
@withinboredom
Copy link
Contributor Author

@folletto

The only frustrating bit is that going back to "All Blocks" loses the scroll position in the list, which makes navigating a few blocks fairly frustrating

Yes, that is really annoying... hopefully I'll figure out a fix soon because it drives me bananas.

@ehg
Copy link
Member

ehg commented Jan 4, 2017

I noticed after cherry-picking #10343, that inline comments aren't being displayed for propTypes documented with inline. e.g. http://calypso.localhost:3000/devdocs/blocks/theme doesn't display a description, though it documents its propTypes: https://github.com/Automattic/wp-calypso/blob/master/client/components/theme/index.jsx#L22

Any idea how we'd fix this?

Copy link
Member

@ehg ehg left a comment

Choose a reason for hiding this comment

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

Looking good so far. I left you some initial pass comments :)

* @param {string} propName The name of the prop
* @return {ReactElement} The row
*/
const row = ( propName ) => {
Copy link
Member

Choose a reason for hiding this comment

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

Could we extract functions that render stuff into components?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes ... I think I was being a little too paranoid about keeping the changes under a couple hundred lines ;)

* @param {string} rightName The prop name of the right side
* @return {number} Which side wins
*/
const sortProps = ( leftName, rightName ) => {
Copy link
Member

Choose a reason for hiding this comment

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

This feels like it should live outside render() — it also makes me think that we're creating a new function per render?

@withinboredom
Copy link
Contributor Author

@ehg said:

inline comments aren't being displayed for propTypes

Looks like it only supports docblocks.

It appears like there was commit in react-docgen near where I'd need to update it to support this as it just strips the comments, if I'm reading it correctly. I don't know how the maintainers feel about it ... so I opened an issue. Short of that, I think a codemod might would work... I know I've thought about a codemod to move the readme to the component at some point in the future, but I don't think I've brought it up, just thought about it.

}
}

PropsViewer.propTypes = {
Copy link
Member

Choose a reason for hiding this comment

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

These could be put in the class as a static property :]

@withinboredom withinboredom force-pushed the add/proptypes-table branch 2 times, most recently from ec1f3d2 to 85b933a Compare January 9, 2017 17:10
/**
* External Dependencies
*/

Copy link
Member

Choose a reason for hiding this comment

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

No space needed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

);
}

static propTypes = {
Copy link
Member

Choose a reason for hiding this comment

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

Any benefits to having these at the bottom, rather than the top of the class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I normally see them outside the class at the bottom of the file. I put them at the bottom so that someone would easily find them out of habit, without searching.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think we only do that because we need Component assigned, before we modify Component.propTypes. With createClass, we'd put them at the top.

Makefile Outdated
@@ -164,7 +164,7 @@ build-dll: node_modules
@mkdir -p build
@CALYPSO_ENV=$(CALYPSO_ENV) $(NODE_BIN)/webpack --display-error-details --config webpack-dll.config.js

build-server: install
build-server: install server/devdocs/proptypes-index.json
Copy link
Member

Choose a reason for hiding this comment

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

What environments will this actually run in? Do we actually need to generate a full proptypes index in the test environment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I realized I forgot to commit my tests, and the build failed without that file existing. I'd like to chat about it in slack if you don't mind. I don't like it.

Copy link
Member

Choose a reason for hiding this comment

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

Did we converge on a solution to this? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, needs mock and this reverted. Still working on it, taking a bit longer than I anticipated.

* @param {object} component The component to render for
* @return {ReactComponent|null} The table or nothing
*/
renderTable = ( component ) => {
Copy link
Member

Choose a reason for hiding this comment

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

Why not use a regular, unbound, function here?


const components = require( '../../../../server/devdocs/proptypes-index.json' );

const findRealComponent = ( slug ) => {
Copy link
Member

Choose a reason for hiding this comment

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

Why are we duplicating code here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My personal strategy with duplicate code is to always duplicate once before refactoring for reuse. I do need to address this.

return null; //todo: explain why this table is missing
}

return (
Copy link
Member

Choose a reason for hiding this comment

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

This still feels a bit long, and unreadable to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does feel a bit long, but it's mostly just markup. Lines 155 - 160 could probably become more readable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could we split this into a few subcomponents? Ideally it'd be nice if we don't fork render logic into separate methods (often that's a sign it should be split to separate components).

it( 'can render itself', () => {
const example = ( <div>Example goes here</div> );
const component = ( <PropsViewer component={ 'props-viewer' } example={ example } /> );
const spection = findRealComponent( 'props-viewer' )[ 0 ];
Copy link
Member

Choose a reason for hiding this comment

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

What's a spection?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Spection comes from Latin specere: to look.
It technically isn't a real word, but one I made up ;)

Copy link
Member

Choose a reason for hiding this comment

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

Hah, I guess I'd like something a little more human readable though. Makes me feel less of a caudex :P

@withinboredom withinboredom force-pushed the add/proptypes-table branch 4 times, most recently from 6801ffa to fd4e5b7 Compare January 18, 2017 18:59
@withinboredom
Copy link
Contributor Author

I moved the build of the json file to only be when make run or make dashboard happens instead of on every build. That would mean it won't show on calypso.live or stage ... so, I'm going to have to find a happy place it should be built at. :grumble: ...

Makefile Outdated
@@ -172,7 +172,7 @@ build: install build-$(CALYPSO_ENV)

build-css: public/style.css public/style-rtl.css public/style-debug.css public/editor.css

build-development: server/devdocs/proptypes-index.json server/devdocs/components-usage-stats.json build-server build-dll $(CLIENT_CONFIG_FILE) server/devdocs/search-index.js build-css
build-development: server/devdocs/components-usage-stats.json build-server build-dll $(CLIENT_CONFIG_FILE) server/devdocs/search-index.js build-css

build-wpcalypso: server/devdocs/proptypes-index.json server/devdocs/components-usage-stats.json build-server build-dll $(CLIENT_CONFIG_FILE) server/devdocs/search-index.js build-css
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now also builds with wpcalypso

ehg
ehg previously requested changes Jan 27, 2017
Makefile Outdated
@@ -91,10 +91,10 @@ welcome:
install: node_modules

# Simply running `make run` will spawn the Node.js server instance.
run: welcome githooks install build
run: welcome githooks install build server/devdocs/proptypes-index.json
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch

Makefile Outdated
@$(NODE) build/bundle-$(CALYPSO_ENV).js

dashboard: install
dashboard: install server/devdocs/proptypes-index.json
Copy link
Member

Choose a reason for hiding this comment

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

Also superfluous :)

@withinboredom withinboredom dismissed ehg’s stale review July 22, 2017 01:29

returning to this since I have a few days and not sure if any issues remain from this review.

@withinboredom
Copy link
Contributor Author

Closing because I suck at making time for this.

@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Oct 3, 2017
@DanReyLop
Copy link
Contributor

DanReyLop commented Oct 22, 2017

In #19064 I first made the generate-proptypes-index task a bit faster in cee40bd (from 30s to about 25s) but then removed it entirely because it's not being used. So if we resurrect this PR, we'll need to add it again, and I recommend using the optimized version. For that, re-add this and these lines to package.json, and restore this file.

@alisterscott alisterscott deleted the add/proptypes-table branch October 24, 2017 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DevDocs OSS Citizen [Size] XL Probably needs to be broken down into multiple smaller issues [Status] Needs Rebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants