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

Add components examples in README.md files - Part 1 #8267

Merged
merged 37 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f98a842
Components examples: Button
mmtr Jul 28, 2018
428135a
Components examples: ButtonGroup
mmtr Jul 28, 2018
2c52190
Components examples: Dashicon
mmtr Jul 28, 2018
dce71d3
Components examples: Disabled
mmtr Jul 29, 2018
a384093
Components examples: Draggable
mmtr Jul 29, 2018
8717496
Components examples: ClipboardButton
mmtr Jul 29, 2018
c62f157
Components examples: CheckboxControl
mmtr Jul 29, 2018
0bfbd6a
Components examples: BaseControl
mmtr Jul 29, 2018
49595f9
Components examples: Autocomplete
mmtr Jul 29, 2018
00e5a40
Components examples: Adding imports and using functional components
mmtr Jul 29, 2018
1c094a4
Components examples: ColorPalette
mmtr Jul 29, 2018
b19d7be
Components examples: DropZone
mmtr Jul 29, 2018
c44be87
Components examples: Dropdown
mmtr Jul 29, 2018
a3772ad
Components examples: DropdownMenu
mmtr Jul 29, 2018
f9d424d
Components examples: ExternalLink
mmtr Jul 29, 2018
9cd06f7
Components examples: FocusableIframe
mmtr Jul 29, 2018
aacf491
Components examples: FontSizePicker
mmtr Jul 29, 2018
781714f
Components examples: FormFileUpload
mmtr Jul 29, 2018
78f9af2
Components examples: FormToggle
mmtr Jul 29, 2018
42cf270
Components examples: FormTokenField
mmtr Jul 29, 2018
ddb5054
Components examples: ColorIndicator
mmtr Jul 29, 2018
0053cfd
Components examples: IconButton
mmtr Jul 29, 2018
2a7cecf
Components examples: KeyboardShortcuts
mmtr Jul 29, 2018
bae58bd
Components examples: MenuGroup and MenuItem
mmtr Jul 29, 2018
8947903
Components examples: MenuItemsChoice
mmtr Jul 29, 2018
be33bc9
Using hashes instead of equality signs for the README main header
mmtr Aug 1, 2018
8a7aaec
Using https://wordpress.org instead of .com
mmtr Aug 1, 2018
033d7df
Using checked property in the CheckboxControl example
mmtr Aug 1, 2018
0928109
Change text used in the ClipboardButton example
mmtr Aug 1, 2018
0d81d0a
Better format in colors array used in the ColorPalette example
mmtr Aug 1, 2018
f2fe641
Placing variable in arrow function between parenthesis
mmtr Aug 1, 2018
7034d81
Add a dynamic message to DropZone example
mmtr Aug 1, 2018
9d86a98
Code style according to linter in FocusableIframe example
mmtr Aug 1, 2018
333e98b
Add missing semicolons to FontSizePicker example
mmtr Aug 1, 2018
dff6984
Add fallbackFontSize to FontSizePicker example
mmtr Aug 1, 2018
dc67ebc
Add missing whitespaces in suggestions array
mmtr Aug 1, 2018
f882482
Use MenuItem in MenuGroup example
mmtr Aug 1, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 51 additions & 26 deletions packages/components/src/autocomplete/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Autocomplete
============
# Autocomplete

This component is used to provide autocompletion support for a child input component.

Expand Down Expand Up @@ -105,33 +104,59 @@ Whether to apply debouncing for the autocompleter. Set to true to enable debounc
- Type: `Boolean`
- Required: No

### Examples
## Usage

The following is a contrived completer for fresh fruit.

```jsx
const fruitCompleter = {
name: 'fruit',
// The prefix that triggers this completer
triggerPrefix: '~',
// The option data
options: [
{ visual: '🍎', name: 'Apple' },
{ visual: '🍊', name: 'Orange' },
{ visual: '🍇', name: 'Grapes' },
],
// Returns a label for an option like "🍊 Orange"
getOptionLabel: option => [
<span class="icon">{ option.visual }</span>,
option.name
],
// Declares that options should be matched by their name
getOptionKeywords: option => [ option.name ],
// Declares that the Grapes option is disabled
isOptionDisabled: option => option.name === 'Grapes',
// Declares completions should be inserted as abbreviations
getOptionCompletion: option => (
<abbr title={ option.name }>{ option.visual }</abbr>
),
import { Autocomplete } from '@wordpress/components';

function FreshFruitAutocomplete() {
Copy link
Member

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.

Copy link
Contributor Author

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/

Copy link
Member

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?

const Wrapper = ({ children }) => (
  <div style={{
    background: 'papayawhip',
    width: '100%',
    padding: '2rem'
  }}>
    {children}
  </div>
)

const Title = () => (
  <h3 style={{ color: 'palevioletred' }}>
    Hello World!
  </h3>
)

render(
  <Wrapper>
    <Title />
  </Wrapper>
)

3rd example on the demo page.

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'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 use render( <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?

Copy link
Member

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.

const autocompleters = [
{
name: 'fruit',
// The prefix that triggers this completer
triggerPrefix: '~',
// The option data
options: [
{ visual: '🍎', name: 'Apple', id: 1 },
{ visual: '🍊', name: 'Orange', id: 2 },
{ visual: '🍇', name: 'Grapes', id: 3 },
],
// Returns a label for an option like "🍊 Orange"
getOptionLabel: option => (
<span>
<span className="icon" >{ option.visual }</span>{ option.name }
</span>
),
// Declares that options should be matched by their name
getOptionKeywords: option => [ option.name ],
// Declares that the Grapes option is disabled
isOptionDisabled: option => option.name === 'Grapes',
// Declares completions should be inserted as abbreviations
getOptionCompletion: option => (
<abbr title={ option.name }>{ option.visual }</abbr>
),
}
];

return (
<div>
<Autocomplete completers={ autocompleters }>
{ ( { isExpanded, listBoxId, activeId } ) => (
<div
contentEditable
suppressContentEditableWarning
aria-autocomplete="list"
aria-expanded={ isExpanded }
aria-owns={ listBoxId }
aria-activedescendant={ activeId }
>
</div>
) }
</Autocomplete>
<p>Type ~ for triggering the autocomplete.</p>
</div>
);
};
```
29 changes: 16 additions & 13 deletions packages/components/src/base-control/README.md
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.

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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?

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, that would be great but I'm afraid I have a limitation here.

I'm using react-live for compiling and running these snippets of code in real time, so we avoid to change our build process.

That forces us to import the dependencies outside the snippets in order to define the scope needed by react-live to run the extracted code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
16 changes: 16 additions & 0 deletions packages/components/src/button-group/README.md
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>
);
}
```
5 changes: 1 addition & 4 deletions packages/components/src/button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ The presence of a `href` prop determines whether an `anchor` element is rendered
Renders a button with default style.

```jsx
/**
* WordPress dependencies
*/
import { Button } from "@wordpress/components";

export default function ClickMeButton() {
function MyButton() {
return (
<Button isDefault>
Click me!
Expand Down
24 changes: 15 additions & 9 deletions packages/components/src/checkbox-control/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
CheckboxControl
=======
# CheckboxControl

CheckboxControl component is used to generate a checkbox input field.

Expand All @@ -8,13 +7,20 @@ CheckboxControl component is used to generate a checkbox input field.

Render an is author checkbox:
```jsx
<CheckboxControl
heading="User"
label="Is author"
help="Is the user a author or not?"
checked={ checked }
onChange={ onChange }
/>
import { CheckboxControl } from '@wordpress/components';
import { withState } from '@wordpress/compose';

withState( {
isChecked: true,
} )( ( { isChecked, setState } ) => (
<CheckboxControl
heading="User"
label="Is author"
help="Is the user a author or not?"
checked={ isChecked }
onChange={ ( isChecked ) => { setState( { isChecked } ) } }
/>
) )
```

## Props
Expand Down
21 changes: 21 additions & 0 deletions packages/components/src/clipboard-button/README.md
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( {
Copy link
Member

Choose a reason for hiding this comment

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

Should we assign it to some const?

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 agree it would make the docs nicer, but react-live won't be able to parse it and render the example. I'll try to figure out an alternative in Calypso so we can have better docs here (as we do with the imports)

hasCopied: false,
} )( ( { hasCopied, setState } ) => (
<ClipboardButton
isPrimary
text="Text to be copied."
onCopy={ () => setState( { hasCopied: true } ) }
onFinishCopy={ () => setState( { hasCopied: false } ) }
>
{ hasCopied ? 'Copied!' : 'Copy Text' }
</ClipboardButton>
) )
```
13 changes: 13 additions & 0 deletions packages/components/src/color-indicator/README.md
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" />
);
}
```
25 changes: 25 additions & 0 deletions packages/components/src/color-palette/README.md
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( {
Copy link
Member

Choose a reason for hiding this comment

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

Should we assign it to some const?

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 } ) }
/>
)
} )
```
17 changes: 17 additions & 0 deletions packages/components/src/dashicon/README.md
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>
);
}
```
3 changes: 1 addition & 2 deletions packages/components/src/date-time/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
DateTimePicker
=======
# DateTimePicker

DateTimePicker is a React component to render a calendar and clock for selecting a date and time. The calendar and clock components can be accessed individually using the `DatePicker` and `TimePicker` components respectively.

Expand Down
31 changes: 16 additions & 15 deletions packages/components/src/disabled/README.md
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.

Expand All @@ -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( {
Copy link
Member

Choose a reason for hiding this comment

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

Should we assign to some const?

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 = () => {
Copy link
Member

Choose a reason for hiding this comment

The 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>
);
} )
Expand Down
23 changes: 23 additions & 0 deletions packages/components/src/draggable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,26 @@ The function called when dragging ends.
- Type: `Function`
- Required: No
- Default: `noop`

## Usage

```jsx
import { Dashicon, Draggable, Panel, PanelBody } from '@wordpress/components';

function DraggablePanel() {
return (
<div id="draggable-panel">
<Panel header="Draggable panel" >
<PanelBody>
<Draggable
elementId="draggable-panel"
transferData={ { } }
>
<Dashicon icon="move" />
</Draggable>
</PanelBody>
</Panel>
</div>
);
}
```
26 changes: 16 additions & 10 deletions packages/components/src/drop-zone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@

```jsx
import { DropZoneProvider, DropZone } from '@wordpress/components';

function MyComponent() {
return (
<DropZoneProvider>
<div>
<DropZone onDrop={ () => console.log( 'do something' ) } />
</div>
</DropZoneProvider>
);
}
import { withState } from '@wordpress/compose';

withState( {
hasDropped: false,
} )( ( { hasDropped, setState } ) => (
<DropZoneProvider>
<div>
{ hasDropped ? 'Dropped!' : 'Drop something here' }
<DropZone
onFilesDrop={ () => setState( { hasDropped: true } ) }
onHTMLDrop={ () => setState( { hasDropped: true } ) }
onDrop={ () => setState( { hasDropped: true } ) }
/>
</div>
</DropZoneProvider>
) );
```

## Props
Expand Down
Loading