|
15 | 15 | <pf-popover heading="Popover heading" |
16 | 16 | body="Popovers are triggered by click rather than hover." |
17 | 17 | footer="Popover footer"> |
18 | | - <button>Toggle popover</button> |
| 18 | + <pf-button>Toggle popover</pf-button> |
19 | 19 | </pf-popover> |
20 | 20 | {% endhtmlexample %} |
21 | 21 |
|
22 | 22 | **Note**: Unlike the [Patternfly React implementation][withfocustrap], this |
23 | 23 | component does not trap focus in the popover dialog. If you would like to trap |
24 | 24 | focus, consider using a modal dialog instead. |
| 25 | + |
| 26 | + ### Activating programmatically |
| 27 | + |
| 28 | + Use the `show()` method to activate the popover. |
| 29 | + |
| 30 | +<pf-tabs class="html-lit-react-snippets"> |
| 31 | + <pf-tab slot="tab">HTML</pf-tab> |
| 32 | + <pf-tab-panel> |
| 33 | + |
| 34 | +```html |
| 35 | +<script type="module"> |
| 36 | + import '@patternfly/elements/pf-button/pf-button.js'; |
| 37 | + import '@patternfly/elements/pf-popover/pf-popover.js'; |
| 38 | +
|
| 39 | + const button = document.querySelector('pf-button'); |
| 40 | +
|
| 41 | + const popover = document.querySelector('pf-popover'); |
| 42 | +
|
| 43 | + button.addEventListener('mouseover', function() { |
| 44 | + popover.show(); |
| 45 | + }); |
| 46 | +
|
| 47 | + button.addEventListener('mouseout', function() { |
| 48 | + popover.hide(); |
| 49 | + }); |
| 50 | +</script> |
| 51 | + |
| 52 | +<pf-button>Hover to cite</pf-button> |
| 53 | + |
| 54 | +<pf-popover> |
| 55 | + <cite slot="body">Richard M. Stallman</cite> |
| 56 | + <q>Free software is a political movement; open source is a development model.</q> |
| 57 | +</pf-popover> |
| 58 | +``` |
| 59 | + |
| 60 | + </pf-tab-panel> |
| 61 | + <pf-tab slot="tab">Lit</pf-tab> |
| 62 | + <pf-tab-panel> |
| 63 | + |
| 64 | +```js |
| 65 | +import { LitElement, html } from 'lit'; |
| 66 | +import '@patternfly/elements/pf-button/pf-button.js'; |
| 67 | +import '@patternfly/elements/pf-popover/pf-popover.js'; |
| 68 | + |
| 69 | +class Citer extends LitElement { |
| 70 | + render() { |
| 71 | + return html` |
| 72 | + <pf-button @mouseover="${this.#onMouseover}" |
| 73 | + @mouseout="${this.#onMouseout}">Hover to Cite</pf-button> |
| 74 | +
|
| 75 | + <pf-popover> |
| 76 | + <cite slot="body">Richard M. Stallman</cite> |
| 77 | + <q>Free software is a political movement; open source is a development model.</q> |
| 78 | + </pf-popover> |
| 79 | + `; |
| 80 | + } |
| 81 | + |
| 82 | + get #popover() { return this.shadowRoot.querySelector('pf-popover'); } |
| 83 | + |
| 84 | + #onMouseover() { this.#popover.show(); } |
| 85 | + |
| 86 | + #onMouseout() { this.#popover.hide(); } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | + </pf-tab-panel> |
| 91 | + <pf-tab slot="tab">React</pf-tab> |
| 92 | + <pf-tab-panel> |
| 93 | + |
| 94 | +```jsx |
| 95 | +import { Button } from '@patternfly/elements/react/pf-button/pf-button.js'; |
| 96 | +import { Popover } from '@patternfly/elements/react/pf-popover/pf-popover.js'; |
| 97 | +import { useRef } from 'react'; |
| 98 | + |
| 99 | + |
| 100 | +export function Citer() { |
| 101 | + const popoverRef = useRef(null); |
| 102 | + |
| 103 | + const onMouseover = e => void popoverRef.current.show(); |
| 104 | + |
| 105 | + const onMouseout = e => void popoverRef.current.hide(); |
| 106 | + |
| 107 | + return ( |
| 108 | + <> |
| 109 | + <Button onmouseover={onMouseover} |
| 110 | + onmouseout={onMouseout}>Hover to Cite</Button> |
| 111 | + <Popover ref={popoverRef}> |
| 112 | + <cite slot="body">Richard M. Stallman</cite> |
| 113 | + <q>Free software is a political movement; open source is a development model.</q> |
| 114 | + </Popover> |
| 115 | + </> |
| 116 | + ); |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | + </pf-tab-panel> |
| 121 | +</pf-tabs> |
25 | 122 | {% endband %} |
26 | 123 |
|
27 | 124 | {% renderSlots %}{% endrenderSlots %} |
|
0 commit comments