Component Framework
Hvad er det?
Static site generator med værktøjer, der gør kode-processen mere belejlig.
Optimerer fx automatisk kode (HTML, CSS & JavaScript) og billeder for dig.
Men det betyder også, at vi skriver kode, der ikke virker i browseren 😬
Dvs. at "live server" heller ikke virker, når vi udvikler i Astro
npm install
Installerer de pakker, som projektet har brug for.
npm run dev (ligesom "live server")
Starter en lokal server, som viser projektet live i browseren
npm create astro@latest
Starter et nyt Astro-projekt med den nyeste version.
<header>
<h1>Heading</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<section id="hero">
<h2>Dette er et Hero-Image</h2>
<p>Her er noget introduktionstekst om denne side.</p>
<button>Lær Mere</button>
</section>
<section id="services">
<h2>Vores Services</h2>
<div class="service">
<h3>Service 1</h3>
<p>Beskrivelse af service 1.</p>
</div>
<div class="service">
<h3>Service 2</h3>
<p>Beskrivelse af service 2.</p>
</div>
<div class="service">
<h3>Service 3</h3>
<p>Beskrivelse af service 3.</p>
</div>
</section>
<footer>
<p>© 2024 Min Side. Alle rettigheder forbeholdes.</p>
</footer>
<header>
<h1>Velkommen til Min Side</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<header>
<h1>Heading</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<section id="hero">
<h2>Dette er et Hero-Image</h2>
<p>Her er noget introduktionstekst om denne side.</p>
<button>Lær Mere</button>
</section>
<section id="services">
<h2>Vores Services</h2>
<div class="service">
<h3>Service 1</h3>
<p>Beskrivelse af service 1.</p>
</div>
<div class="service">
<h3>Service 2</h3>
<p>Beskrivelse af service 2.</p>
</div>
<div class="service">
<h3>Service 3</h3>
<p>Beskrivelse af service 3.</p>
</div>
</section>
<footer>
<p>© 2024 Min Side. Alle rettigheder forbeholdes.</p>
</footer>
<section id="hero">
<h2>Dette er et Hero-Image</h2>
<p>Her er noget introduktionstekst om denne side.</p>
<button>Lær Mere</button>
</section>
<div class="service">
<h3>Service 1</h3>
<p>Beskrivelse af service 1.</p>
</div>
<section id="services">
<h2>Vores Services</h2>
<Service />
<Service />
<Service />
</section>
<header>
<h1>Velkommen til Min Side</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<header>
<h1>Heading</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<section id="hero">
<h2>Dette er et Hero-Image</h2>
<p>Her er noget introduktionstekst om denne side.</p>
<button>Lær Mere</button>
</section>
<section id="services">
<h2>Vores Services</h2>
<div class="service">
<h3>Service 1</h3>
<p>Beskrivelse af service 1.</p>
</div>
<div class="service">
<h3>Service 2</h3>
<p>Beskrivelse af service 2.</p>
</div>
<div class="service">
<h3>Service 3</h3>
<p>Beskrivelse af service 3.</p>
</div>
</section>
<footer>
<p>© 2024 Min Side. Alle rettigheder forbeholdes.</p>
</footer>
<section id="hero">
<h2>Dette er et Hero-Image</h2>
<p>Her er noget introduktionstekst om denne side.</p>
<button>Lær Mere</button>
</section>
<div class="service">
<h3>Service 1</h3>
<p>Beskrivelse af service 1.</p>
</div>
<section id="services">
<h2>Vores Services</h2>
<Service />
<Service />
<Service />
</section>
<header>
<h1>Velkommen til Min Side</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<section id="hero">
<h2>Dette er et Hero-Image</h2>
<p>Her er noget introduktionstekst om denne side.</p>
<button>Lær Mere</button>
</section>
<div class="service">
<h3>Service 1</h3>
<p>Beskrivelse af service 1.</p>
</div>
<section id="services">
<h2>Vores Services</h2>
<Service />
<Service />
<Service />
</section>
<Layout>
<Hero />
<Services />
</Layout>
<header>
<h1>Heading</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<section id="hero">
<h2>Dette er et Hero-Image</h2>
<p>Her er noget introduktionstekst om denne side.</p>
<button>Lær Mere</button>
</section>
<section id="services">
<h2>Vores Services</h2>
<div class="service">
<h3>Service 1</h3>
<p>Beskrivelse af service 1.</p>
</div>
<div class="service">
<h3>Service 2</h3>
<p>Beskrivelse af service 2.</p>
</div>
<div class="service">
<h3>Service 3</h3>
<p>Beskrivelse af service 3.</p>
</div>
</section>
<footer>
<p>© 2024 Min Side. Alle rettigheder forbeholdes.</p>
</footer>
<Layout>
<Hero />
<Services />
</Layout>
<header>
<h1>Heading</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<section id="hero">
<h2>Dette er et Hero-Image</h2>
<p>Her er noget introduktionstekst om denne side.</p>
<button>Lær Mere</button>
</section>
<section id="services">
<h2>Vores Services</h2>
<div class="service">
<h3>Service 1</h3>
<p>Beskrivelse af service 1.</p>
</div>
<div class="service">
<h3>Service 2</h3>
<p>Beskrivelse af service 2.</p>
</div>
<div class="service">
<h3>Service 3</h3>
<p>Beskrivelse af service 3.</p>
</div>
</section>
<footer>
<p>© 2024 Min Side. Alle rettigheder forbeholdes.</p>
</footer>
øvelse
Identificer components sammen med din sidemakker i følgende. Hvor ser I components?
øvelse
øvelse
Lav Astro-components ud fra en eksisterende HTML-fil (se øvelsen på Fronter)
Hvor ser vi components?
Header.astro
Button.astro
Footer.astro
Card.astro
CardSection.astro
Hero.astro
Astro-components
├── src/
└── components/
└── Header.astro
└── CardSection.astro
└──
└── Button.astro
└── Footer.astro
Card.astro
Astro-components
<li>
<h3>Animation</h3>
<p>
Learn the latest animation techniques to create stunning motion
design and captivate your audience.
</p>
<a href="/animation" class="btn" data-variant="ghost">Get started</a>
</li>
Card.astro
Astro-components
---
import Card from "../components/ ";
---
<Card />
<Card />
<Card />
<Card />
<Card />
<Card />
Card.astro
Astro-components
---
---
<article>
<h2></h2>
<p></p>
</article>
<style>
article {...}
</style>
"Single file component", som betyder, at vi samler al relevant HTML, CSS (og JavaScript) i den enkelte component
Astro-components
---
---
<article>
<h2></h2>
<p></p>
</article>
<style>
article {...}
</style>
<script>
...
</script>
"Single file component", som betyder, at vi samler al relevant HTML, CSS (og JavaScript) i den enkelte component
Astro-components og pages
---
---
"Frontmatter"
import "../styles/global.css"
---
---
import "../styles/global.css"
import Component from "../components/Component.astro"
Importer stylesheet
Astro-components og pages
---
---
import "../styles/global.css"
import Component from "../components/Component.astro"
const { prop } = Astro.props;
Importer component
Astro-components og pages
---
---
import "../styles/global.css"
import Component from "../components/Component.astro"
const { prop } = Astro.props;
Saml props op
const now = new Date();
const localTime = now.toLocaleTimeString("da-DK");
Astro-components og pages
---
---
import "../styles/global.css"
const { prop } = Astro.props;
JavaScript på serveren ("build time")
const now = new Date();
const localTime = now.toLocaleTimeString("da-DK");
Astro-components og pages
---
---
<p>Hej, klokken er {localTime}</p>
JavaScript på serveren ("build time")
const now = new Date();
const localTime = now.toLocaleTimeString("da-DK");
Hvad var klokken, der jeg byggede siden (statisk)
Astro-components og pages
Props
---
const { title, description } = Astro.props;
---
<article>
<h2>{title}</h2>
<p>{description}</p>
</article>
Saml props op
Props
---
const { title, description } = Astro.props;
---
<article>
<h2>{title}</h2>
<p>{description}</p>
</article>
Saml props op
---
import Card from "../components/Card.astro";
---
<Card title="Jeg er en overskrift"
description="Jeg er en beskrivelse"
/>
Send props til Card-component
Skabelon
Props
---
import Card from "../components/Card.astro";
---
<Card title="Jeg er en overskrift"
description="Jeg er en beskrivelse"
/>
<Card title="Jeg er anderledes"
description="Også mig..."
/>
Scoped styles
---
const { title, description } = Astro.props;
---
<article>
<h2>{title}</h2>
<p>{description}</p>
</article>
<style>
h2 {
color: red;
}
</style>
Brug Card
<ul>
<Card iconName="running-man"
title="Animation"
description="Learn the latest..."
link="/animation"
/>
<!-- ... -->
</ul>
Brug Card
<ul>
<Card iconName="img"
title="Design"
description="Create beautiful, usable..."
link="/design"
/>
<!-- ... -->
</ul>
Styling af Card
<!-- Astro Props & HTML for Card -->
<style>
li {
display: flex;
flex-flow: column;
aspect-ratio: 1;
padding: 2rem;
background: #fff;
border-radius: 0.5rem;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
0 10px 10px -5px rgba(0, 0, 0, 0.04);
}
</style>
Styling af Card
<!-- Astro Props & HTML for Card -->
<style>
li {
display: flex;
flex-flow: column;
aspect-ratio: 1;
padding: 2rem;
background: #fff;
border-radius: 0.5rem;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
0 10px 10px -5px rgba(0, 0, 0, 0.04);
}
</style>
Påvirker kun `li` i Card, ikke andre
Styling af Card
<!-- Astro Props & HTML for Card -->
<style>
h3 {
color: #000;
font-size: 1.25rem;
margin: 0;
}
</style>
Påvirker kun `h3` i Card, ikke andre
Components
Design Patterns
Grid System
Design Tokens
Hierarki
Components
Design Patterns
Hierarki
Grid System
Design Tokens
Components
Components
<Button>Knap</Button>
npm create astro@latest
npm create astro@latest
figma
npm create astro@latest
HOME
PRODUCTS
ABOUT
npm create astro@latest
HOME
PRODUCTS
ABOUT
<Button variant="secondary">Action</Button>
secondary
primary
secondary
primary
---
const { variant } = Astro.props;
---
<button class={variant}>
<slot />
</button>
<style>
.secondary {
background: red;
}
</style>
dynamisk
skabelon
<Button variant="secondary">Knap</Button>
<Button variant="secondary">Knap</Button>
<Button variant="secondary">Knap</Button>
<Button variant="secondary">Send</Button>
<button class="secondary">Send</button>
<Button hasIcon={true}>Send</Button>
<button>
<span>Send</span>
<svg>...</svg>
</button>
<Button link="/send" hasIcon={true}>Send</Button>
<a href="/send">
<span>Send</span>
<svg>...</svg>
</a>
Hvis "link", skift til korrekt HTML-tag
<Button variant="primary"
hasIcon={true}
icon="arrow-right"
link="/send"
>
Send
</Button>
<button>
<span>Send</span>
<svg>...</svg>
</button>
<ul>
<Card title="Animation"
description="Learn the latest..."
link="/animation"
/>
<!-- flere Cards -->
</ul>
<!-- ... -->
<ul>
<li>
<h3>Animation</h3>
<p>
Learn the latest animation techniques.
</p>
<a href="/animation" class="ghost">
Get started
</a>
</li>
<!-- flere Cards -->
</ul>
<!-- ... -->
Button.astro
<li>
<h3>{title}</h3>
<p>
{description}
</p>
<Button variant="ghost">Get started</Button>
</li>
Button.astro
---
import Button from "./components/Button.astro"
---
<li>
<h3>{title}</h3>
<p>
{description}
</p>
<Button variant="ghost">Get started</Button>
</li>
---
import Button from "./components/Button.astro"
---
<li>
<h3>{title}</h3>
<p>
{description}
</p>
<Button variant="ghost">Get started</Button>
</li>
---
import Button from "./components/Button.astro"
---
<li>
<h3>{title}</h3>
<p>
{description}
</p>
<Button variant="ghost">Get started</Button>
</li>
Nested component
øvelse
Brug Astro til at genskabe så meget af open.spotify.com som du kan (se øvelsen på Fronter)
npm create astro@latest .
npm create astro@latest .
<Button
variant="primary">
Log in
</Button>
<Button
variant="primary"
size="medium">
Log in
</Button>
---
const { variant } = Astro.props;
---
<button class={variant}>
<slot />
</button>
<style>
.primary { ... }
</style>
---
const { variant, size } = Astro.props;
---
<button class={`${variant} ${size}`}>
<slot />
</button>
<style>
.primary { ... }
.medium { ... }
</style>
Push dit site til Netlify