Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Commit b32b8b6

Browse files
author
Emanuel Suriano
committed
demo: add dynamic sections in demo
1 parent 467f625 commit b32b8b6

File tree

6 files changed

+63
-9
lines changed

6 files changed

+63
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"scripts": {
1515
"build": "yarn workspace react-scroll-section build",
16-
"build:watch": "yarn workspace react-scroll-section build --watch",
16+
"build:watch": "yarn workspace react-scroll-section build --watch --minify false",
1717
"start": "yarn workspace demo dev",
1818
"build:demo": "yarn build && yarn workspace demo build",
1919
"release": "yarn build && yarn workspace react-scroll-section release",

packages/demo/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@types/styled-components": "^5.1.26",
1313
"prop-types": "^15.8.1",
14+
"randomcolor": "^0.6.2",
1415
"react": "17.0.2",
1516
"react-dom": "17.0.2",
1617
"react-github-corner": "^2.5.0",
@@ -19,6 +20,7 @@
1920
"styled-components": "5.3.6"
2021
},
2122
"devDependencies": {
23+
"@types/randomcolor": "^0.5.6",
2224
"@types/react": "18.0.21",
2325
"@types/react-dom": "18.0.6",
2426
"@types/react-toggle": "^4.0.3",

packages/demo/src/App.tsx

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import { useState } from 'react';
22
import { ScrollingProvider, Section } from 'react-scroll-section';
3+
import randomColor from 'randomcolor';
34
import ModeToggle, { Mode } from './ModeToggle';
45
import { DynamicMenu, StaticMenu } from './Menu';
56
import { Footer, Menu, SectionContainer } from './Builders';
67
import logo from './logo.svg';
78

89
function App() {
910
const [mode, setMode] = useState<Mode>('dynamic');
11+
const [sections, setSections] = useState<string[]>([]);
1012

1113
return (
1214
<ScrollingProvider>
13-
<Menu>{mode === 'static' ? <StaticMenu /> : <DynamicMenu />}</Menu>
15+
<Menu>
16+
{mode === 'static' ? (
17+
<StaticMenu />
18+
) : (
19+
<DynamicMenu
20+
onAdd={() =>
21+
setSections((prev) => [...prev, `Section ${sections.length + 1}`])
22+
}
23+
/>
24+
)}
25+
</Menu>
1426

1527
<Section id="home">
1628
<SectionContainer>
@@ -22,29 +34,47 @@ function App() {
2234
</Section>
2335

2436
<Section id="about">
25-
<SectionContainer background="accent2">
37+
<SectionContainer background={randomColor({ seed: 'about' })}>
2638
<span role="img" aria-label="hands up">
2739
🙋‍♂️
2840
</span>
2941
</SectionContainer>
3042
</Section>
3143

3244
<Section id="projects">
33-
<SectionContainer background="accent3">
45+
<SectionContainer background={randomColor({ seed: 'projects' })}>
3446
<span role="img" aria-label="computer">
3547
💻
3648
</span>
3749
</SectionContainer>
3850
</Section>
3951

4052
<Section id="contact">
41-
<SectionContainer>
53+
<SectionContainer background={randomColor({ seed: 'contact' })}>
4254
<span role="img" aria-label="letter">
4355
💌
4456
</span>
4557
</SectionContainer>
4658
</Section>
4759

60+
{sections.map((name) => (
61+
<Section key={name} id={name}>
62+
<SectionContainer background={randomColor({ seed: name })}>
63+
<span>
64+
{name}{' '}
65+
<sup
66+
aria-label="Remove section"
67+
onClick={() =>
68+
setSections((prev) => prev.filter((id) => id !== name))
69+
}
70+
>
71+
x
72+
</sup>
73+
</span>
74+
</SectionContainer>
75+
</Section>
76+
))}
77+
4878
<Footer>
4979
<ModeToggle mode={mode} onChange={setMode} />
5080
<a href="https://www.netlify.com">

packages/demo/src/Builders.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const Item = styled.li<{ selected: boolean }>`
2727
`;
2828

2929
export const SectionContainer = styled.section<{
30-
background?: keyof DefaultTheme;
30+
background?: string;
3131
}>`
3232
min-height: 100vh;
3333
min-width: 320px;
@@ -39,15 +39,20 @@ export const SectionContainer = styled.section<{
3939
text-align: center;
4040
scroll-behavior: smooth;
4141
position: 'relative';
42-
background: ${(props) => props.theme[props.background || 'background']};
42+
background: ${(props) => props.background || props.theme.background};
4343
4444
& h1 {
4545
font-size: 2em;
4646
}
4747
48-
& span[role='img'] {
48+
& span {
4949
font-size: 4em;
5050
}
51+
52+
& sup {
53+
font-size: 0.5em;
54+
cursor: pointer;
55+
}
5156
`;
5257

5358
export const Footer = styled.div`

packages/demo/src/Menu.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { Item } from './Builders';
22
import { useScrollSection, useScrollSections } from 'react-scroll-section';
33

4-
export const DynamicMenu = () => {
4+
type Props = {
5+
onAdd: () => void;
6+
};
7+
8+
export const DynamicMenu = ({ onAdd }: Props) => {
59
const sections = useScrollSections();
610

711
return (
@@ -11,6 +15,9 @@ export const DynamicMenu = () => {
1115
{id.toUpperCase()}
1216
</Item>
1317
))}
18+
<Item selected={false} onClick={onAdd} aria-label="Add section">
19+
+
20+
</Item>
1421
</>
1522
);
1623
};

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@
539539
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf"
540540
integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==
541541

542+
"@types/randomcolor@^0.5.6":
543+
version "0.5.6"
544+
resolved "https://registry.yarnpkg.com/@types/randomcolor/-/randomcolor-0.5.6.tgz#c1085077c4ae0f6af60e0ad229d14721b9b3b58c"
545+
integrity sha512-lKkW8DGUQpZldTrwa+HM5rY+7eTyaHOMTsnj9ewt7AAwXuMPwBmkLlfh8+SXdgOhBW9iNI4x4zRjQ/TQZkdycQ==
546+
542547
543548
version "18.0.6"
544549
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-18.0.6.tgz#36652900024842b74607a17786b6662dd1e103a1"
@@ -2237,6 +2242,11 @@ queue-microtask@^1.2.2:
22372242
resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243"
22382243
integrity sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==
22392244

2245+
randomcolor@^0.6.2:
2246+
version "0.6.2"
2247+
resolved "https://registry.yarnpkg.com/randomcolor/-/randomcolor-0.6.2.tgz#7a57362ae1a1278439aeed2c15e5deb8ea33f56d"
2248+
integrity sha512-Mn6TbyYpFgwFuQ8KJKqf3bqqY9O1y37/0jgSK/61PUxV4QfIMv0+K2ioq8DfOjkBslcjwSzRfIDEXfzA9aCx7A==
2249+
22402250
22412251
version "17.0.2"
22422252
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-17.0.2.tgz#ecffb6845e3ad8dbfcdc498f0d0a939736502c23"

0 commit comments

Comments
 (0)