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

HStack: Convert component to TypeScript #41580

Merged
merged 6 commits into from
Jun 30, 2022

Conversation

ralucaStan
Copy link
Contributor

@ralucaStan ralucaStan commented Jun 7, 2022

What?

Converts the HStack component to TypeScript

Part of #35744

Why?

Part of Contributor Day at WordCamp EU 2022

How?

I followed the Transition to TS Guide.

Testing Instructions

  1. Run npm run dev and ensure no typing errors.
  2. Run npm run storybook:dev and visit the HStack component.

cc @ciampo

Screenshots or screencast

@ralucaStan ralucaStan requested a review from ajitbohra as a code owner June 7, 2022 16:35
@walbo walbo added [Type] Enhancement A suggestion for improvement. [Feature] UI Components Impacts or related to the UI component system [Type] Code Quality Issues or PRs that relate to code quality [Package] Components /packages/components labels Jun 7, 2022
@ciampo ciampo requested review from mirka and ciampo June 9, 2022 09:52
@ciampo ciampo requested a review from chad1008 June 9, 2022 13:23
Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

Thank you so much @ralucaStan for your contribution — it's greatly appreciated ❤️

Apart from a few inline comments, the main thing I noticed is that currently the Storybook pages haven't been setup in a way that shows the component's props as docs:

Screenshot 2022-06-09 at 16 00 25

In order to fix this, we'll need to make a few changes:

  • Add a named export for the component (on top of the default one
  • Add a few settings to the Storybook example's config
  • Use the ComponentMeta and ComponentStory types from Storybook
  • Rewrite the story in an "controls-friendly" way (e.g. having a template, and using it to instantiate individual stories)
  • Tweak the control types (and whether certain controls should be hidden)

(All of these points should be explained in the guide, but please let me know in case they were not explained in a clear way!)

Applying the above suggestion may result in these changes
diff --git a/packages/components/src/h-stack/component.tsx b/packages/components/src/h-stack/component.tsx
index 1db3155e1b..ee7c36ace8 100644
--- a/packages/components/src/h-stack/component.tsx
+++ b/packages/components/src/h-stack/component.tsx
@@ -6,7 +6,7 @@ import { View } from '../view';
 import { useHStack } from './hook';
 import type { Props } from './types';
 
-function HStack(
+function UnconnectedHStack(
 	props: WordPressComponentProps< Props, 'div' >,
 	forwardedRef: React.ForwardedRef< any >
 ) {
@@ -39,6 +39,6 @@ function HStack(
  * ```
  */
 
-const ConnectedHStack = contextConnect( HStack, 'HStack' );
+export const HStack = contextConnect( UnconnectedHStack, 'HStack' );
 
-export default ConnectedHStack;
+export default HStack;
diff --git a/packages/components/src/h-stack/stories/index.tsx b/packages/components/src/h-stack/stories/index.tsx
index a0b672035b..16331ceb8f 100644
--- a/packages/components/src/h-stack/stories/index.tsx
+++ b/packages/components/src/h-stack/stories/index.tsx
@@ -1,22 +1,93 @@
+/**
+ * External dependencies
+ */
+import type { ComponentMeta, ComponentStory } from '@storybook/react';
+
 /**
  * Internal dependencies
  */
 import { View } from '../../view';
 import { HStack } from '..';
 
-export default {
+const ALIGNMENTS = {
+	top: 'top',
+	topLeft: 'topLeft',
+	topRight: 'topRight',
+	left: 'left',
+	center: 'center',
+	right: 'right',
+	bottom: 'bottom',
+	bottomLeft: 'bottomLeft',
+	bottomRight: 'bottomRight',
+	edge: 'edge',
+	stretch: 'stretch',
+};
+
+const DIRECTIONS = {
+	row: 'row',
+	column: 'column',
+	responsive: [ 'column', 'row' ],
+};
+
+const JUSTIFICATIONS = {
+	'space-around': 'space-around',
+	'space-between': 'space-between',
+	'space-evenly': 'space-evenly',
+	stretch: 'stretch',
+	center: 'center',
+	end: 'end',
+	'flex-end': 'flex-end',
+	'flex-start': 'flex-start',
+	start: 'start',
+};
+
+const meta: ComponentMeta< typeof HStack > = {
 	component: HStack,
 	title: 'Components (Experimental)/HStack',
+	argTypes: {
+		as: {
+			control: { type: null },
+		},
+		children: {
+			control: { type: null },
+		},
+		alignment: {
+			control: { type: 'select' },
+			options: Object.keys( ALIGNMENTS ),
+			mapping: ALIGNMENTS,
+		},
+		direction: {
+			control: { type: 'select' },
+			options: Object.keys( DIRECTIONS ),
+			mapping: DIRECTIONS,
+		},
+		justify: {
+			control: { type: 'select' },
+			options: Object.keys( JUSTIFICATIONS ),
+			mapping: JUSTIFICATIONS,
+		},
+		spacing: {
+			control: { type: 'text' },
+		},
+	},
+	parameters: {
+		controls: { expanded: true },
+		docs: { source: { state: 'open' } },
+	},
 };
+export default meta;
+
+const Template: ComponentStory< typeof HStack > = ( args ) => (
+	<HStack { ...args } style={ { background: '#eee', minHeight: '3rem' } }>
+		{ [ 'One', 'Two', 'Three', 'Four', 'Five' ].map( ( text ) => (
+			<View key={ text } style={ { background: '#b9f9ff' } }>
+				{ text }
+			</View>
+		) ) }
+	</HStack>
+);
 
-export const _default = () => {
-	return (
-		<HStack spacing={ 3 }>
-			<View>One</View>
-			<View>Two</View>
-			<View>Three</View>
-			<View>Four</View>
-			<View>Five</View>
-		</HStack>
-	);
+export const Default: ComponentStory< typeof HStack > = Template.bind( {} );
+Default.args = {
+	spacing: '3',
 };

Finally, could you add an entry to the package's CHANGELOG ?

Thank you for your patience!

packages/components/src/h-stack/utils.ts Show resolved Hide resolved
packages/components/src/h-stack/utils.ts Outdated Show resolved Hide resolved
packages/components/src/ui/utils/get-valid-children.ts Outdated Show resolved Hide resolved
packages/components/src/ui/utils/get-valid-children.ts Outdated Show resolved Hide resolved
packages/components/src/h-stack/hook.tsx Show resolved Hide resolved
@ralucaStan ralucaStan force-pushed the update/h-stack-typescript branch from 50f3587 to 9aafaa9 Compare June 24, 2022 13:04
@ralucaStan ralucaStan requested a review from ciampo June 24, 2022 13:42
@ralucaStan
Copy link
Contributor Author

@ciampo thank you so much for your support with this PR. I have addressed the requested changes and the Story for this component now works. Please take a look again.

Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

Code changes LGTM, tests well in Storybook 🚀

Thank you so so much for contributing to the components package! Hopefully this will be first of many others 🤞

@ciampo ciampo merged commit 744b1bf into WordPress:trunk Jun 30, 2022
@github-actions github-actions bot added this to the Gutenberg 13.7 milestone Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] UI Components Impacts or related to the UI component system [Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants