|
1 | | -# PatternFly Element | Dropdown Element |
2 | | - |
| 1 | +# PatternFly Elements Dropdown |
| 2 | + |
3 | 3 | This element will provide a dropdown menu of links and/or actions. It's comprised of one sub-component, `pfe-dropdown-item` , which denotes an item in the dropdown menu. |
4 | 4 |
|
5 | | -## Usage |
| 5 | +Read more about Dropdown in the [PatternFly Elements Dropdown documentation](https://patternflyelements.org/components/dropdown) |
6 | 6 |
|
7 | | -``` html |
8 | | -<pfe-dropdown label="Dropdown"> |
9 | | - <pfe-dropdown-item item-type="link"> |
10 | | - <a href="https://bit.ly/3b9wvWg">Link 1</a> |
11 | | - </pfe-dropdown-item> |
12 | | - <pfe-dropdown-item item-type="link"> |
13 | | - <a href="https://bit.ly/3b9wvWg">Link 2</a> |
14 | | - </pfe-dropdown-item> |
15 | | - <pfe-dropdown-item item-type="link" disabled> |
16 | | - <a href="https://bit.ly/3b9wvWg">Link 2</a> |
17 | | - </pfe-dropdown-item> |
18 | | - <pfe-dropdown-item item-type="separator"></pfe-dropdown-item> |
19 | | - <pfe-dropdown-item item-type="action"> |
20 | | - <button>Action 1</button> |
21 | | - </pfe-dropdown-item> |
22 | | -</pfe-dropdown> |
23 | | -``` |
| 7 | +## Installation |
24 | 8 |
|
25 | | -You can also provide a list of dropdown items dynamically: |
| 9 | +Load `<pfe-dropdown>` via CDN: |
26 | 10 |
|
27 | | -``` js |
28 | | -let dropdown = document.querySelector("pfe-dropdown"); |
| 11 | +```html |
| 12 | +<script src="https://unpkg.com/@patternfly/pfe-dropdown?module"></script> |
29 | 13 | ``` |
30 | 14 |
|
31 | | -When pfe-dropdown is defined, via the `whenDefined` method. Pass an array of pfe-dropdown-item objects to `pfeDropdownOptions`. |
| 15 | +Or, if you are using [NPM](https://npm.im), install it |
32 | 16 |
|
33 | | -``` js |
34 | | -customElements.whenDefined("pfe-dropdown").then(function() { |
35 | | - dropdown.pfeDropdownOptions = [ |
36 | | - { |
37 | | - href: "https://bit.ly/3b9wvWg", |
38 | | - text: "Link 1", |
39 | | - type: "link", |
40 | | - disabled: false |
41 | | - }, |
42 | | - { |
43 | | - href: "https://bit.ly/3b9wvWg", |
44 | | - text: "Link 2", |
45 | | - type: "link", |
46 | | - disabled: false |
47 | | - }, |
48 | | - { |
49 | | - href: "https://bit.ly/3b9wvWg", |
50 | | - text: "Link 3", |
51 | | - type: "link", |
52 | | - disabled: true |
53 | | - }, |
54 | | - { |
55 | | - type: "separator" |
56 | | - }, |
57 | | - { |
58 | | - text: "Action 1", |
59 | | - type: "action", |
60 | | - disabled: false |
61 | | - }, |
62 | | - { |
63 | | - text: "Action 2", |
64 | | - type: "action", |
65 | | - disabled: true |
66 | | - } |
67 | | - ]; |
68 | | -}); |
| 17 | +```bash |
| 18 | +npm install @patternfly/pfe-dropdown |
69 | 19 | ``` |
70 | 20 |
|
71 | | -Or you can add individual dropdown items with the `addDropdownOptions` method. Pass an array of pfe-dropdown-item objects to `addDropdownOptions` . |
| 21 | +Then once installed, import it to your application: |
72 | 22 |
|
73 | | -``` js |
74 | | -customElements.whenDefined("pfe-dropdown").then(function() { |
75 | | - dropdown.addDropdownOptions( |
76 | | - [{ |
77 | | - href: "https://bit.ly/3b9wvWg", |
78 | | - text: "Link 4", |
79 | | - type: "link", |
80 | | - disabled: false |
81 | | - }] |
82 | | - ); |
83 | | -}); |
| 23 | +```js |
| 24 | +import '@patternfly/pfe-dropdown'; |
84 | 25 | ``` |
85 | 26 |
|
86 | | -## Slots |
87 | | - |
88 | | -### Default slot |
89 | | - |
90 | | -The default slot should contain at least one link or action `pfe-dropdown-item` . |
91 | | - |
92 | | -## Attributes |
93 | | - |
94 | | -### pfe-dropdown |
95 | | - |
96 | | -* `label` : This is an optional attribute string that you can provide to describe your dropdown, which appears in the dropdown toggle. |
97 | | -* `disabled` : This is an optional attribute that you can provide to disable your dropdown. Visually the dropdown will look disabled and mouse or keyboard events will have no impact on it. |
98 | | - |
99 | | -### pfe-dropdown-item |
100 | | - |
101 | | -* `item-type` : This is an optional attribute string that you should provide to indicate the type of dropdown item. This drives the appropriate assignment of accessibility attributes for each type of item. |
102 | | - |
103 | | - - `link` : an HTML link |
104 | | - - `action` : a button that triggers some sort of action |
105 | | - - `separator` : a visual separator for items in the list |
106 | | - |
107 | | -## API |
108 | | - |
109 | | -### open |
110 | | -Manually opens the dropdown menu. |
111 | | -`document.querySelector("pfe-dropdown").open()` ; |
112 | | - |
113 | | -### close |
114 | | - |
115 | | -Manually closes the dropdown menu. |
116 | | -`document.querySelector("pfe-dropdown").close()` ; |
117 | | - |
118 | | -## Events |
119 | | - |
120 | | -* `pfe-dropdown:change` : When an item is selected, this event is fired. It includes the inner text of the item that was selected. |
121 | | - |
122 | | -## Test |
123 | | - |
124 | | - npm run test |
125 | | - |
126 | | -## Build |
127 | | - |
128 | | - npm run build |
| 27 | +## Usage |
129 | 28 |
|
130 | | -## Demo |
| 29 | +``` html |
| 30 | +<pfe-dropdown label="Dropdown"> |
| 31 | + <pfe-dropdown-item item-type="link"> |
| 32 | + <a href="https://bit.ly/3b9wvWg">Link 1</a> |
| 33 | + </pfe-dropdown-item> |
| 34 | + <pfe-dropdown-item item-type="link"> |
| 35 | + <a href="https://bit.ly/3b9wvWg">Link 2</a> |
| 36 | + </pfe-dropdown-item> |
| 37 | + <pfe-dropdown-item item-type="link" disabled> |
| 38 | + <a href="https://bit.ly/3b9wvWg">Link 2</a> |
| 39 | + </pfe-dropdown-item> |
| 40 | + <pfe-dropdown-item item-type="separator"></pfe-dropdown-item> |
| 41 | + <pfe-dropdown-item item-type="action"> |
| 42 | + <button>Action 1</button> |
| 43 | + </pfe-dropdown-item> |
| 44 | +</pfe-dropdown> |
| 45 | +``` |
131 | 46 |
|
132 | | -From the PFElements root directory, run: |
| 47 | +You can also provide a list of dropdown items dynamically: |
133 | 48 |
|
134 | | - npm start |
| 49 | +``` js |
| 50 | +const dropdown = document.querySelector("pfe-dropdown"); |
| 51 | +dropdown.options = [ |
| 52 | + { |
| 53 | + href: "https://bit.ly/3b9wvWg", |
| 54 | + text: "Link 1", |
| 55 | + type: "link", |
| 56 | + disabled: false |
| 57 | + }, |
| 58 | + { |
| 59 | + href: "https://bit.ly/3b9wvWg", |
| 60 | + text: "Link 2", |
| 61 | + type: "link", |
| 62 | + disabled: false |
| 63 | + }, |
| 64 | + { |
| 65 | + href: "https://bit.ly/3b9wvWg", |
| 66 | + text: "Link 3", |
| 67 | + type: "link", |
| 68 | + disabled: true |
| 69 | + }, |
| 70 | + { |
| 71 | + type: "separator" |
| 72 | + }, |
| 73 | + { |
| 74 | + text: "Action 1", |
| 75 | + type: "action", |
| 76 | + disabled: false |
| 77 | + }, |
| 78 | + { |
| 79 | + text: "Action 2", |
| 80 | + type: "action", |
| 81 | + disabled: true |
| 82 | + } |
| 83 | +]; |
| 84 | +``` |
135 | 85 |
|
136 | | -## Code style |
| 86 | +Or you can add individual dropdown items with the `addDropdownOptions` method. Pass an array of pfe-dropdown-item objects to `addDropdownOptions` . |
137 | 87 |
|
138 | | -Dropdown (and all PFElements) use [Prettier][prettier] to auto-format JS and JSON. The style rules get applied when you commit a change. If you choose to, you can [integrate your editor][prettier-ed] with Prettier to have the style rules applied on every save. |
| 88 | +``` js |
| 89 | +await dropdown.updateComplete; |
| 90 | +dropdown.addDropdownOptions([{ |
| 91 | + href: "https://bit.ly/3b9wvWg", |
| 92 | + text: "Link 4", |
| 93 | + type: "link", |
| 94 | + disabled: false |
| 95 | +}]); |
| 96 | +``` |
139 | 97 |
|
140 | | -[prettier]: https://github.com/prettier/prettier/ |
141 | | -[prettier-ed]: https://prettier.io/docs/en/editors.html |
142 | | -[web-component-tester]: https://github.com/Polymer/web-component-tester |
0 commit comments