|
1 | 1 | import Flickity from '../src/index'; |
2 | 2 | import './App.css'; |
3 | | -import React from 'react'; |
| 3 | +import React, { useState } from 'react'; |
| 4 | +import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; |
| 5 | +import { oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism'; |
4 | 6 |
|
5 | 7 | import Default from './Default'; |
6 | 8 | import Static from './Static'; |
7 | 9 |
|
8 | 10 | function App() { |
| 11 | + const [activeTab, setActiveTab] = useState('examples'); |
| 12 | + |
| 13 | + const apiDocs = { |
| 14 | + installation: { |
| 15 | + title: 'Installation', |
| 16 | + bash: `# Install flickity as peer dependency (use v2.3.0 for best experience) |
| 17 | +npm install flickity@2.3.0 |
| 18 | +npm install react-flickity-component` |
| 19 | + }, |
| 20 | + basicUsage: { |
| 21 | + title: 'Basic Usage', |
| 22 | + javascript: `// CommonJS |
| 23 | +const Flickity = require('react-flickity-component'); |
| 24 | +
|
| 25 | +// ES2015 module |
| 26 | +import Flickity from 'react-flickity-component' |
| 27 | +
|
| 28 | +const flickityOptions = { |
| 29 | + initialIndex: 2 |
| 30 | +} |
| 31 | +
|
| 32 | +function Carousel() { |
| 33 | + return ( |
| 34 | + <Flickity |
| 35 | + className={'carousel'} |
| 36 | + elementType={'div'} |
| 37 | + options={flickityOptions} |
| 38 | + disableImagesLoaded |
| 39 | + reloadOnUpdate |
| 40 | + static |
| 41 | + > |
| 42 | + <img src="/images/placeholder.png"/> |
| 43 | + <img src="/images/placeholder.png"/> |
| 44 | + <img src="/images/placeholder.png"/> |
| 45 | + </Flickity> |
| 46 | + ) |
| 47 | +}` |
| 48 | + }, |
| 49 | + props: { |
| 50 | + title: 'Props', |
| 51 | + content: `# Props |
| 52 | +
|
| 53 | +| Property | Type | Default | Description | |
| 54 | +|----------|------|---------|-------------| |
| 55 | +| \`className\` | \`String\` | \`''\` | Applied to top level wrapper | |
| 56 | +| \`elementType\` | \`String\` | \`'div'\` | Wrapper's element type | |
| 57 | +| \`options\` | \`Object\` | \`{}\` | Flickity initialization options | |
| 58 | +| \`disableImagesLoaded\` | \`Boolean\` | \`false\` | Disable call \`reloadCells\` images are loaded | |
| 59 | +| \`flickityRef\` | \`Function\` | | Like \`ref\` function, get Flickity instance in parent component | |
| 60 | +| \`reloadOnUpdate\` | \`Boolean\` | \`false\` | Run \`reloadCells\` and \`resize\` on \`componentDidUpdate\` | |
| 61 | +| \`static\` | \`Boolean\` | \`false\` | Carousel contents are static and not updated at runtime |` |
| 62 | + }, |
| 63 | + apiEvents: { |
| 64 | + title: 'API & Events', |
| 65 | + description: 'You can access the Flickity instance with `flickityRef` prop and use this instance to register events and use API.', |
| 66 | + javascript: `// Function component |
| 67 | +function Carousel() { |
| 68 | + const ref = React.useRef(null); |
| 69 | +
|
| 70 | + const myCustomNext = () => { |
| 71 | + // You can use Flickity API |
| 72 | + ref.current.next() |
| 73 | + } |
| 74 | +
|
| 75 | + React.useEffect(() => { |
| 76 | + if (ref.current) { |
| 77 | + ref.current.on("settle", () => { |
| 78 | + console.log(\`current index is \${ref.current.selectedIndex}\`); |
| 79 | + }); |
| 80 | + } |
| 81 | + }, []); |
| 82 | +
|
| 83 | + return ( |
| 84 | + <> |
| 85 | + <Flickity flickityRef={c => ref.current = c}> |
| 86 | + <img src="/images/placeholder.png"/> |
| 87 | + <img src="/images/placeholder.png"/> |
| 88 | + <img src="/images/placeholder.png"/> |
| 89 | + </Flickity> |
| 90 | + <Button onClick={myCustomNext}>My custom next button</Button> |
| 91 | + </> |
| 92 | + ) |
| 93 | +}` |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + const CodeBlock = ({ code, language }: { code: string; language: string }) => ( |
| 98 | + <SyntaxHighlighter |
| 99 | + language={language} |
| 100 | + style={oneDark} |
| 101 | + customStyle={{ |
| 102 | + borderRadius: '8px', |
| 103 | + fontSize: '14px', |
| 104 | + margin: '1rem 0' |
| 105 | + }} |
| 106 | + > |
| 107 | + {code} |
| 108 | + </SyntaxHighlighter> |
| 109 | + ); |
| 110 | + |
9 | 111 | return ( |
10 | 112 | <> |
11 | 113 | <header> |
12 | 114 | <nav> |
13 | | - <a href="https://github.com/yaodingyd/react-flickity-component"> |
14 | | - Github |
15 | | - </a> |
| 115 | + <ul> |
| 116 | + <li><a href="https://github.com/yaodingyd/react-flickity-component" target="_blank" rel="noopener noreferrer">GitHub</a></li> |
| 117 | + <li><a href="https://www.npmjs.com/package/react-flickity-component" target="_blank" rel="noopener noreferrer">NPM</a></li> |
| 118 | + </ul> |
16 | 119 | </nav> |
17 | | - <h1>React-Flickity-Component</h1> |
| 120 | + <h1>React Flickity Component</h1> |
| 121 | + <p>A React.js component for <a href="https://flickity.metafizzy.co/" target="_blank" rel="noopener noreferrer">Flickity</a> to build touch-friendly carousels</p> |
18 | 122 | </header> |
19 | 123 |
|
20 | 124 | <main> |
21 | | - <Default /> |
22 | | - <Static /> |
| 125 | + <nav className="tab-nav"> |
| 126 | + <button |
| 127 | + className={activeTab === 'examples' ? 'active' : ''} |
| 128 | + onClick={() => setActiveTab('examples')} |
| 129 | + > |
| 130 | + Examples |
| 131 | + </button> |
| 132 | + <button |
| 133 | + className={activeTab === 'docs' ? 'active' : ''} |
| 134 | + onClick={() => setActiveTab('docs')} |
| 135 | + > |
| 136 | + Documentation |
| 137 | + </button> |
| 138 | + </nav> |
| 139 | + |
| 140 | + {activeTab === 'examples' && ( |
| 141 | + <section className="examples-section"> |
| 142 | + <Default /> |
| 143 | + <Static /> |
| 144 | + </section> |
| 145 | + )} |
| 146 | + |
| 147 | + {activeTab === 'docs' && ( |
| 148 | + <section className="docs-section"> |
| 149 | + <nav className="docs-nav"> |
| 150 | + <ul> |
| 151 | + <li><a href="#installation">Installation</a></li> |
| 152 | + <li><a href="#basic-usage">Basic Usage</a></li> |
| 153 | + <li><a href="#props">Props</a></li> |
| 154 | + <li><a href="#api-events">API & Events</a></li> |
| 155 | + </ul> |
| 156 | + </nav> |
| 157 | + |
| 158 | + <div className="docs-content"> |
| 159 | + <article id="installation"> |
| 160 | + <h2>{apiDocs.installation.title}</h2> |
| 161 | + <CodeBlock code={apiDocs.installation.bash} language="bash" /> |
| 162 | + </article> |
| 163 | + |
| 164 | + <article id="basic-usage"> |
| 165 | + <h2>{apiDocs.basicUsage.title}</h2> |
| 166 | + <CodeBlock code={apiDocs.basicUsage.javascript} language="javascript" /> |
| 167 | + </article> |
| 168 | + |
| 169 | + <article id="props"> |
| 170 | + <h2>Props</h2> |
| 171 | + <table> |
| 172 | + <thead> |
| 173 | + <tr> |
| 174 | + <th>Property</th> |
| 175 | + <th>Type</th> |
| 176 | + <th>Default</th> |
| 177 | + <th>Description</th> |
| 178 | + </tr> |
| 179 | + </thead> |
| 180 | + <tbody> |
| 181 | + <tr><td><code>className</code></td><td><code>String</code></td><td><code>''</code></td><td>Applied to top level wrapper</td></tr> |
| 182 | + <tr><td><code>elementType</code></td><td><code>String</code></td><td><code>'div'</code></td><td>Wrapper's element type</td></tr> |
| 183 | + <tr><td><code>options</code></td><td><code>Object</code></td><td><code>{}</code></td><td>Flickity initialization options</td></tr> |
| 184 | + <tr><td><code>disableImagesLoaded</code></td><td><code>Boolean</code></td><td><code>false</code></td><td>Disable call <code>reloadCells</code> images are loaded</td></tr> |
| 185 | + <tr><td><code>flickityRef</code></td><td><code>Function</code></td><td></td><td>Like <code>ref</code> function, get Flickity instance in parent component</td></tr> |
| 186 | + <tr><td><code>reloadOnUpdate</code></td><td><code>Boolean</code></td><td><code>false</code></td><td>Run <code>reloadCells</code> and <code>resize</code> on <code>componentDidUpdate</code></td></tr> |
| 187 | + <tr><td><code>static</code></td><td><code>Boolean</code></td><td><code>false</code></td><td>Carousel contents are static and not updated at runtime</td></tr> |
| 188 | + </tbody> |
| 189 | + </table> |
| 190 | + </article> |
| 191 | + |
| 192 | + <article id="api-events"> |
| 193 | + <h2>{apiDocs.apiEvents.title}</h2> |
| 194 | + <p>{apiDocs.apiEvents.description}</p> |
| 195 | + <CodeBlock code={apiDocs.apiEvents.javascript} language="javascript" /> |
| 196 | + </article> |
| 197 | + </div> |
| 198 | + </section> |
| 199 | + )} |
23 | 200 | </main> |
| 201 | + |
| 202 | + <footer> |
| 203 | + <p><a href="https://github.com/yaodingyd/react-flickity-component">Contribute on GitHub</a></p> |
| 204 | + </footer> |
24 | 205 | </> |
25 | 206 | ); |
26 | 207 | } |
|
0 commit comments