|
| 1 | +--- |
| 2 | +title: React Offcanvas Component |
| 3 | +name: Offcanvas |
| 4 | +description: React alert component allows build hidden sidebars into your project for navigation, shopping carts. |
| 5 | +menu: Components |
| 6 | +route: /components/offcanvas |
| 7 | +--- |
| 8 | + |
| 9 | +import { Playground, Props } from 'docz' |
| 10 | +import { useState } from 'react' |
| 11 | +import { CCallout } from '../callout/CCallout' |
| 12 | + |
| 13 | +import { CButton } from '../button/CButton' |
| 14 | +import { CButtonClose } from '../button/CButtonClose' |
| 15 | +import { COffcanvas } from './COffcanvas' |
| 16 | +import { COffcanvasBody } from './COffcanvasBody' |
| 17 | +import { COffcanvasHeader } from './COffcanvasHeader' |
| 18 | +import { COffcanvasTitle } from './COffcanvasTitle' |
| 19 | + |
| 20 | +## Examples |
| 21 | + |
| 22 | +### Offcanvas components |
| 23 | + |
| 24 | +Below is an offcanvas example that is shown by default (via `.show` on `.offcanvas`). Offcanvas includes support for a header with a close button and an optional body class for some initial `padding`. We suggest that you include offcanvas headers with dismiss actions whenever possible, or provide an explicit dismiss action. |
| 25 | + |
| 26 | +<Playground> |
| 27 | + <div className="docs-example-offcanvas bg-light"> |
| 28 | + <COffcanvas backdrop={false} placement="start" portal={false} visible={true}> |
| 29 | + <COffcanvasHeader> |
| 30 | + <COffcanvasTitle>Offcanvas</COffcanvasTitle> |
| 31 | + <CButtonClose className="text-reset" /> |
| 32 | + </COffcanvasHeader> |
| 33 | + <COffcanvasBody> |
| 34 | + Content for the offcanvas goes here. You can place just about any Bootstrap component or |
| 35 | + custom elements here. |
| 36 | + </COffcanvasBody> |
| 37 | + </COffcanvas> |
| 38 | + </div> |
| 39 | +</Playground> |
| 40 | + |
| 41 | +### Live demo |
| 42 | + |
| 43 | +Use the buttons below to show and hide an offcanvas element via JavaScript that toggles the `.show` class on an element with the `.offcanvas` class. |
| 44 | + |
| 45 | +- `.offcanvas` hides content (default) |
| 46 | +- `.offcanvas.show` shows content |
| 47 | + |
| 48 | +You can use a link with the `href` attribute, or a button with the `data-coreui-target` attribute. In both cases, the `data-coreui-toggle="offcanvas"` is required. |
| 49 | + |
| 50 | +<Playground> |
| 51 | + {() => { |
| 52 | + const [visible, setVisible] = useState(false) |
| 53 | + return ( |
| 54 | + <> |
| 55 | + <CButton onClick={() => setVisible(true)}>Toggle offcanvas</CButton> |
| 56 | + <COffcanvas placement="start" visible={visible} onDismiss={() => setVisible(false)} > |
| 57 | + <COffcanvasHeader> |
| 58 | + <COffcanvasTitle>Offcanvas</COffcanvasTitle> |
| 59 | + <CButtonClose className="text-reset" onClick={() => setVisible(false)}/> |
| 60 | + </COffcanvasHeader> |
| 61 | + <COffcanvasBody> |
| 62 | + Content for the offcanvas goes here. You can place just about any Bootstrap component or |
| 63 | + custom elements here. |
| 64 | + </COffcanvasBody> |
| 65 | + </COffcanvas> |
| 66 | + </> |
| 67 | + ) |
| 68 | + }} |
| 69 | +</Playground> |
| 70 | + |
| 71 | +## Placement |
| 72 | + |
| 73 | +There's no default placement for offcanvas components, so you must add one of the modifier classes below; |
| 74 | + |
| 75 | +- `.offcanvas-start` places offcanvas on the left of the viewport (shown above) |
| 76 | +- `.offcanvas-end` places offcanvas on the right of the viewport |
| 77 | +- `.offcanvas-top` places offcanvas on the top of the viewport |
| 78 | +- `.offcanvas-bottom` places offcanvas on the bottom of the viewport |
| 79 | + |
| 80 | +Try the top, right, and bottom examples out below. |
| 81 | + |
| 82 | +<Playground> |
| 83 | + {() => { |
| 84 | + const [visible, setVisible] = useState(false) |
| 85 | + return ( |
| 86 | + <> |
| 87 | + <CButton onClick={() => setVisible(true)}>Toggle top offcanvas</CButton> |
| 88 | + <COffcanvas placement="top" visible={visible} onDismiss={() => setVisible(false)} > |
| 89 | + <COffcanvasHeader> |
| 90 | + <COffcanvasTitle>Offcanvas</COffcanvasTitle> |
| 91 | + <CButtonClose className="text-reset" onClick={() => setVisible(false)}/> |
| 92 | + </COffcanvasHeader> |
| 93 | + <COffcanvasBody> |
| 94 | + Content for the offcanvas goes here. You can place just about any Bootstrap component or |
| 95 | + custom elements here. |
| 96 | + </COffcanvasBody> |
| 97 | + </COffcanvas> |
| 98 | + </> |
| 99 | + ) |
| 100 | + }} |
| 101 | +</Playground> |
| 102 | + |
| 103 | +<Playground> |
| 104 | + {() => { |
| 105 | + const [visible, setVisible] = useState(false) |
| 106 | + return ( |
| 107 | + <> |
| 108 | + <CButton onClick={() => setVisible(true)}>Toggle right offcanvas</CButton> |
| 109 | + <COffcanvas placement="end" visible={visible} onDismiss={() => setVisible(false)} > |
| 110 | + <COffcanvasHeader> |
| 111 | + <COffcanvasTitle>Offcanvas</COffcanvasTitle> |
| 112 | + <CButtonClose className="text-reset" onClick={() => setVisible(false)}/> |
| 113 | + </COffcanvasHeader> |
| 114 | + <COffcanvasBody> |
| 115 | + Content for the offcanvas goes here. You can place just about any Bootstrap component or |
| 116 | + custom elements here. |
| 117 | + </COffcanvasBody> |
| 118 | + </COffcanvas> |
| 119 | + </> |
| 120 | + ) |
| 121 | + }} |
| 122 | +</Playground> |
| 123 | + |
| 124 | +<Playground> |
| 125 | + {() => { |
| 126 | + const [visible, setVisible] = useState(false) |
| 127 | + return ( |
| 128 | + <> |
| 129 | + <CButton onClick={() => setVisible(true)}>Toggle bottom offcanvas</CButton> |
| 130 | + <COffcanvas placement="bottom" visible={visible} onDismiss={() => setVisible(false)} > |
| 131 | + <COffcanvasHeader> |
| 132 | + <COffcanvasTitle>Offcanvas</COffcanvasTitle> |
| 133 | + <CButtonClose className="text-reset" onClick={() => setVisible(false)}/> |
| 134 | + </COffcanvasHeader> |
| 135 | + <COffcanvasBody> |
| 136 | + Content for the offcanvas goes here. You can place just about any Bootstrap component or |
| 137 | + custom elements here. |
| 138 | + </COffcanvasBody> |
| 139 | + </COffcanvas> |
| 140 | + </> |
| 141 | + ) |
| 142 | + }} |
| 143 | +</Playground> |
0 commit comments