-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add components examples in README.md files - Part 1 #8267
Changes from all commits
f98a842
428135a
2c52190
dce71d3
a384093
8717496
c62f157
0bfbd6a
49595f9
00e5a40
1c094a4
b19d7be
c44be87
a3772ad
f9d424d
9cd06f7
aacf491
781714f
78f9af2
42cf270
ddb5054
0053cfd
2a7cecf
bae58bd
8947903
be33bc9
8a7aaec
033d7df
0928109
0d81d0a
f2fe641
7034d81
9d86a98
333e98b
dff6984
dc67ebc
f882482
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
BaseControl | ||
======= | ||
# BaseControl | ||
|
||
BaseControl component is used to generate labels and help text for components handling user inputs. | ||
|
||
|
@@ -8,17 +7,21 @@ BaseControl component is used to generate labels and help text for components ha | |
|
||
Render a BaseControl for a textarea input: | ||
```jsx | ||
<BaseControl | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also include import statement to make it possible to extract code as is and run it without any modifications? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that would be great but I'm afraid I have a limitation here. I'm using That forces us to import the dependencies outside the snippets in order to define the scope needed by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anyway, I think we should maintain them in the examples in order to provide documentation as complete as possible. We can always ignore them when running the snippets (you can check how I'm handling that here: https://github.com/Automattic/wp-calypso/pull/26367/files#diff-d0c3bde7d6504cb5de69c0488b877342R36) |
||
id="textarea-1" | ||
label="Text" | ||
help="Enter some text" | ||
> | ||
<textarea | ||
id="textarea-1" | ||
onChange={ onChangeValue } | ||
value={ value } | ||
/> | ||
</BaseControl> | ||
import { BaseControl } from '@wordpress/components'; | ||
|
||
function MyBaseControl() { | ||
return ( | ||
<BaseControl | ||
id="textarea-1" | ||
label="Text" | ||
help="Enter some text" | ||
> | ||
<textarea | ||
id="textarea-1" | ||
/> | ||
</BaseControl> | ||
); | ||
} | ||
``` | ||
|
||
## Props | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# ButtonGroup | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { Button, ButtonGroup } from '@wordpress/components'; | ||
|
||
function MyButtonGroup() { | ||
return ( | ||
<ButtonGroup> | ||
<Button isPrimary>Button 1</Button> | ||
<Button isPrimary>Button 2</Button> | ||
</ButtonGroup> | ||
); | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# ClipboardButton | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { ClipboardButton } from '@wordpress/components'; | ||
import { withState } from '@wordpress/compose'; | ||
|
||
withState( { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we assign it to some There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree it would make the docs nicer, but |
||
hasCopied: false, | ||
} )( ( { hasCopied, setState } ) => ( | ||
<ClipboardButton | ||
isPrimary | ||
text="Text to be copied." | ||
onCopy={ () => setState( { hasCopied: true } ) } | ||
onFinishCopy={ () => setState( { hasCopied: false } ) } | ||
> | ||
{ hasCopied ? 'Copied!' : 'Copy Text' } | ||
</ClipboardButton> | ||
) ) | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# ColorIndicator | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { ColorIndicator } from '@wordpress/components'; | ||
|
||
function MyColorIndicator() { | ||
return ( | ||
<ColorIndicator colorValue="#f00" /> | ||
); | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# ColorPalette | ||
|
||
## Usage | ||
```jsx | ||
import { ColorPalette } from '@wordpress/components'; | ||
import { withState } from '@wordpress/compose'; | ||
|
||
withState( { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we assign it to some |
||
color: '#f00', | ||
} )( ( { color, setState } ) => { | ||
const colors = [ | ||
{ name: 'red', color: '#f00' }, | ||
{ name: 'white', color: '#fff' }, | ||
{ name: 'blue', color: '#00f' }, | ||
]; | ||
|
||
return ( | ||
<ColorPalette | ||
colors={ colors } | ||
value={ color } | ||
onChange={ color => setState( { color } ) } | ||
/> | ||
) | ||
} ) | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Dashicon | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { Dashicon } from '@wordpress/components'; | ||
|
||
function MyDashicons() { | ||
return ( | ||
<div> | ||
<Dashicon icon="admin-home" /> | ||
<Dashicon icon="products" /> | ||
<Dashicon icon="wordpress" /> | ||
</div> | ||
); | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
Disabled | ||
======== | ||
# Disabled | ||
|
||
Disabled is a component which disables descendant tabbable elements and prevents pointer interaction. | ||
|
||
|
@@ -8,25 +7,27 @@ Disabled is a component which disables descendant tabbable elements and prevents | |
Assuming you have a form component, you can disable all form inputs by wrapping the form with `<Disabled>`. | ||
|
||
```jsx | ||
const DisableToggleForm = withState( { | ||
isDisabled: true, | ||
} )( ( { isDisabled, setState } ) => { | ||
let form = <form><input /></form>; | ||
import { Button, Disabled, TextControl } from '@wordpress/components'; | ||
import { withState } from '@wordpress/compose'; | ||
|
||
withState( { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we assign to some |
||
isDisabled: true, | ||
} )( ( { isDisabled, setState } ) => { | ||
let input = <TextControl label="Input" onChange={ () => {} } />; | ||
if ( isDisabled ) { | ||
form = <Disabled>{ form }</Disabled>; | ||
input = <Disabled>{ input }</Disabled>; | ||
} | ||
|
||
const toggleDisabled = setState( ( state ) => ( { | ||
isDisabled: ! state.isDisabled, | ||
} ) ); | ||
|
||
const toggleDisabled = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch 👍 |
||
setState( ( state ) => ( { isDisabled: ! state.isDisabled } ) ); | ||
}; | ||
return ( | ||
<div> | ||
{ form } | ||
<button onClick={ toggleDisabled }> | ||
{ input } | ||
<Button isPrimary onClick={ toggleDisabled }> | ||
Toggle Disabled | ||
</button> | ||
</Button> | ||
</div> | ||
); | ||
} ) | ||
|
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.
Out of curiosity, don't you need to export those components to be able to use them in Calypso devdocs? For the purpose of docs it's fine as is. However, if that would help, don't hesitate to add export statements.
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.
react-live
doesn't need it. As long as you inject a string with either a component class, a functional component or JSX code, it will be able to render it: https://react-live.kitten.sh/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.
How do you feel about using the version which calls
render
?3rd example on the demo page.
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.
It's not trivial.
That
render
call should be performed in Calypso (it doesn't make sense to include it in the Gutenberg examples), but how Calypso can know what are the components that need to be rendered?For rendering the Autocomplete example it should use
render( <FreshFruitAutocomplete /> )
, but for the Button example it should userender( <MyButton /> )
. But there is no way for getting this in a dynamic way.Maybe we can use always the
My<ComponentName>
pattern and assume that in Calypso. But we need to be sure that the name of the variables doesn't change in the example or we'll broke the Calypso's DevDocs. What do you think?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 missed that comment. It seems like a good pattern, let's discuss further in the 2nd part of the PR where those changes are applied.