|
1 |
| -import React from "react"; |
| 1 | +import React, { useState, useRef, useCallback } from "react"; |
2 | 2 | import styled from "styled-components";
|
3 | 3 |
|
4 | 4 | import Reviews from "./Reviews";
|
5 | 5 | import Companies from "./Companies";
|
6 | 6 | import People from "./People";
|
7 | 7 | import Playground from "./Playground";
|
| 8 | +import ComputationTime from "./ComputationTime"; |
8 | 9 |
|
9 | 10 | import CodeReviews from "./code/Reviews";
|
10 | 11 | import CodeCompanies from "./code/Companies";
|
11 | 12 | import CodePeople from "./code/People";
|
12 | 13 |
|
| 14 | +import { __safePerformanceNow } from "../lib/util"; |
| 15 | + |
13 | 16 | const Separator = styled.div`
|
14 |
| - height: ${props => props.height}px; |
| 17 | + height: ${(props) => props.height}px; |
| 18 | +`; |
| 19 | + |
| 20 | +const AlertBox = styled.div` |
| 21 | + border: 3px rgb(255, 190, 110, 0.5) solid; |
| 22 | + background: rgb(255, 190, 110, 0.1); |
| 23 | + border-radius: 0.75rem; |
| 24 | +
|
| 25 | + p { |
| 26 | + margin: 1.5rem; |
| 27 | + } |
15 | 28 | `;
|
16 | 29 |
|
| 30 | +const AlertNote = ({ children }) => <AlertBox>{children}</AlertBox>; |
| 31 | + |
17 | 32 | const CodeNote = ({ url }) => (
|
18 | 33 | <p>
|
19 |
| - Have a look{" "} |
20 |
| - <a href={url} target="_blank"> |
21 |
| - at the original, full code for this example |
22 |
| - </a>{" "} |
23 |
| - or just the basic, simplified gist: |
| 34 | + Here is the essential code for the above example, or you can have a more in depth look{" "} |
| 35 | + <a href={url} target="_blank" rel="noreferrer"> |
| 36 | + at the original, full code |
| 37 | + </a> |
| 38 | + : |
24 | 39 | </p>
|
25 | 40 | );
|
26 | 41 |
|
27 |
| -const CompiledDemo = () => ( |
28 |
| - <div> |
29 |
| - <h2>Companies</h2> |
30 |
| - <Reviews /> |
31 |
| - <Separator height={15} /> |
32 |
| - <CodeNote url="https://github.com/mxmzb/react-marquee-slider/blob/master/example/src/components/reviews.js" /> |
33 |
| - <CodeReviews /> |
34 |
| - <Separator height={50} /> |
35 |
| - |
36 |
| - <h2>Companies</h2> |
37 |
| - <Companies /> |
38 |
| - <Separator height={15} /> |
39 |
| - <CodeNote url="https://github.com/mxmzb/react-marquee-slider/blob/master/example/src/components/companies.js" /> |
40 |
| - <CodeCompanies /> |
41 |
| - <Separator height={50} /> |
42 |
| - |
43 |
| - <h2>People</h2> |
44 |
| - <People /> |
45 |
| - <Separator height={15} /> |
46 |
| - <CodeNote url="https://github.com/mxmzb/react-marquee-slider/blob/master/example/src/components/people.js" /> |
47 |
| - <CodePeople /> |
48 |
| - <Separator height={50} /> |
49 |
| - |
50 |
| - <h2>Playground</h2> |
51 |
| - <Playground /> |
52 |
| - </div> |
53 |
| -); |
| 42 | +const CompiledDemo = () => { |
| 43 | + const t0 = useRef({}); |
| 44 | + const [perfData, setPerfData] = useState({}); |
| 45 | + |
| 46 | + const onReviewsStartPerformance = useCallback( |
| 47 | + () => (t0.current = Object.assign({}, t0.current, { reviews: __safePerformanceNow() })), |
| 48 | + [], |
| 49 | + ); |
| 50 | + |
| 51 | + const onReviewsEndPerformance = useCallback(({ totalTries }) => { |
| 52 | + setPerfData((prevPerfData) => |
| 53 | + Object.assign({}, prevPerfData, { |
| 54 | + reviews: { |
| 55 | + computationTime: Math.floor(__safePerformanceNow() - t0.current["reviews"]), |
| 56 | + totalTries, |
| 57 | + }, |
| 58 | + }), |
| 59 | + ); |
| 60 | + }, []); |
| 61 | + |
| 62 | + const onCompaniesStartPerformance = useCallback( |
| 63 | + () => (t0.current = Object.assign({}, t0.current, { companies: __safePerformanceNow() })), |
| 64 | + [], |
| 65 | + ); |
| 66 | + |
| 67 | + const onCompaniesEndPerformance = useCallback(({ totalTries }) => { |
| 68 | + console.log("onCompaniesEndPerformance.totalTries", totalTries); |
| 69 | + console.log("onCompaniesEndPerformance.t0", t0); |
| 70 | + setPerfData((prevPerfData) => |
| 71 | + Object.assign({}, prevPerfData, { |
| 72 | + companies: { |
| 73 | + computationTime: Math.floor(__safePerformanceNow() - t0.current["companies"]), |
| 74 | + totalTries, |
| 75 | + }, |
| 76 | + }), |
| 77 | + ); |
| 78 | + }, []); |
| 79 | + |
| 80 | + const onPeopleStartPerformance = useCallback( |
| 81 | + () => (t0.current = Object.assign({}, t0.current, { people: __safePerformanceNow() })), |
| 82 | + [], |
| 83 | + ); |
| 84 | + |
| 85 | + const onPeopleEndPerformance = useCallback(({ totalTries }) => { |
| 86 | + setPerfData((prevPerfData) => |
| 87 | + Object.assign({}, prevPerfData, { |
| 88 | + people: { |
| 89 | + computationTime: Math.floor(__safePerformanceNow() - t0.current["people"]), |
| 90 | + totalTries, |
| 91 | + }, |
| 92 | + }), |
| 93 | + ); |
| 94 | + }, []); |
| 95 | + |
| 96 | + const onPlaygroundStartPerformance = useCallback( |
| 97 | + () => (t0.current = Object.assign({}, t0.current, { playground: __safePerformanceNow() })), |
| 98 | + [], |
| 99 | + ); |
| 100 | + |
| 101 | + const onPlaygroundEndPerformance = useCallback(({ totalTries }) => { |
| 102 | + setPerfData((prevPerfData) => |
| 103 | + Object.assign({}, prevPerfData, { |
| 104 | + playground: { |
| 105 | + computationTime: Math.floor(__safePerformanceNow() - t0.current["playground"]), |
| 106 | + totalTries, |
| 107 | + }, |
| 108 | + }), |
| 109 | + ); |
| 110 | + }, []); |
| 111 | + |
| 112 | + console.log("actual perfData", perfData); |
| 113 | + |
| 114 | + return ( |
| 115 | + <div> |
| 116 | + <AlertNote> |
| 117 | + <p> |
| 118 | + <b>Note:</b> The flickering ("jumping") of the items in the Marquee is not a bug. When you |
| 119 | + want to position items inside the slider randomly, the slider needs to actually render |
| 120 | + them, to see if the random positioning will fit inside the given area. If it doesn't (e.g. |
| 121 | + items are overlapping), it tries to find another place for the overlapping item. Depending |
| 122 | + on the number and size of items, and the size of the available space, overlapping items |
| 123 | + are more or less likely. |
| 124 | + </p> |
| 125 | + <p> |
| 126 | + You can hide the flickering by overlaying the slider with a loading div, until it |
| 127 | + finishes. The slider provides an <code>onFinish</code> callback for this very reason. You |
| 128 | + can see an example with an overlaying loading div further down. |
| 129 | + </p> |
| 130 | + </AlertNote> |
| 131 | + <h2>Reviews</h2> |
| 132 | + <Reviews |
| 133 | + onStartPerformance={onReviewsStartPerformance} |
| 134 | + onEndPerformance={onReviewsEndPerformance} |
| 135 | + /> |
| 136 | + <Separator height={15} /> |
| 137 | + <ComputationTime perfData={perfData.reviews} /> |
| 138 | + <Separator height={15} /> |
| 139 | + <CodeNote url="https://github.com/mxmzb/react-marquee-slider/blob/master/example/src/components/reviews.js" /> |
| 140 | + <CodeReviews /> |
| 141 | + <Separator height={50} /> |
| 142 | + <h2>Companies</h2> |
| 143 | + <Companies |
| 144 | + onStartPerformance={onCompaniesStartPerformance} |
| 145 | + onEndPerformance={onCompaniesEndPerformance} |
| 146 | + /> |
| 147 | + <Separator height={15} /> |
| 148 | + <ComputationTime perfData={perfData.companies} /> |
| 149 | + <Separator height={15} /> |
| 150 | + <CodeNote url="https://github.com/mxmzb/react-marquee-slider/blob/master/example/src/components/companies.js" /> |
| 151 | + <CodeCompanies /> |
| 152 | + <Separator height={50} /> |
| 153 | + <h2>People</h2> |
| 154 | + <People |
| 155 | + onStartPerformance={onPeopleStartPerformance} |
| 156 | + onEndPerformance={onPeopleEndPerformance} |
| 157 | + /> |
| 158 | + <Separator height={15} /> |
| 159 | + <ComputationTime instant /> |
| 160 | + <Separator height={15} /> |
| 161 | + <CodeNote url="https://github.com/mxmzb/react-marquee-slider/blob/master/example/src/components/people.js" /> |
| 162 | + <CodePeople /> |
| 163 | + <Separator height={50} /> |
| 164 | + <h2>Playground</h2> |
| 165 | + <Playground |
| 166 | + onStartPerformance={onPlaygroundStartPerformance} |
| 167 | + onEndPerformance={onPlaygroundEndPerformance} |
| 168 | + perfData={perfData.playground} |
| 169 | + /> |
| 170 | + </div> |
| 171 | + ); |
| 172 | +}; |
54 | 173 |
|
55 | 174 | export default CompiledDemo;
|
0 commit comments