Feat: navigation par slides horizontaux
All checks were successful
Deploy / Deploy to Production (push) Successful in 14s
All checks were successful
Deploy / Deploy to Production (push) Successful in 14s
- Nouveau store slides.svelte.js : gestion de l'état des slides (activeIndex, pendingPath, chargement progressif) - Réécriture du router : remplace navaid par une logique custom avec chargement de la slide initiale puis des autres en arrière-plan - App.svelte : layout slides-wrapper avec translateX, transition 1000ms - Header.svelte : menu 100% dynamique depuis slides.all (ordre et titres multilingues depuis Kirby) - site/controllers/site.php : navigation exposée via site->pages()->listed() avec titres par langue, home prependue Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
614098baf6
commit
8e01cbe611
5 changed files with 241 additions and 174 deletions
|
|
@ -1,6 +1,11 @@
|
|||
<?php
|
||||
|
||||
return function ($page, $kirby, $site) {
|
||||
$homePage = $site->find('home');
|
||||
$navPages = $homePage
|
||||
? $site->pages()->listed()->prepend($homePage->id(), $homePage)
|
||||
: $site->pages()->listed();
|
||||
|
||||
// Generic page data available in all templates
|
||||
$genericData = [
|
||||
'title' => $page->title()->value(),
|
||||
|
|
@ -19,13 +24,19 @@ return function ($page, $kirby, $site) {
|
|||
'name' => $l->name()
|
||||
];
|
||||
})->values(),
|
||||
'navigation' => $site->main_navigation()->toStructure()->map(function($item) use ($kirby) {
|
||||
$linkedPage = $item->link()->toPages()->first();
|
||||
'navigation' => $navPages->map(function($p) use ($kirby) {
|
||||
$titles = [];
|
||||
foreach ($kirby->languages() as $lang) {
|
||||
$titles[$lang->code()] = $p->content($lang->code())->title()->value();
|
||||
}
|
||||
if (empty($titles)) {
|
||||
$titles['fr'] = $p->title()->value();
|
||||
}
|
||||
return [
|
||||
'label_fr' => $item->label_fr()->value(),
|
||||
'label_en' => $item->label_en()->value(),
|
||||
'url' => $linkedPage?->url(),
|
||||
'isActive' => $linkedPage?->isOpen() ?? false
|
||||
'id' => $p->uid(),
|
||||
'url' => '/' . $p->uri(),
|
||||
'title' => $p->title()->value(),
|
||||
'titles' => $titles
|
||||
];
|
||||
})->values()
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
<script>
|
||||
import { page } from '@state/page.svelte'
|
||||
import { slides } from '@state/slides.svelte'
|
||||
|
||||
import Header from '@components/layout/Header.svelte'
|
||||
import Footer from '@components/layout/Footer.svelte'
|
||||
import Cursor from '@components/layout/Cursor.svelte'
|
||||
|
||||
import Home from '@views/Home.svelte'
|
||||
|
|
@ -17,22 +16,20 @@
|
|||
import Default from '@views/Default.svelte'
|
||||
|
||||
const templates = {
|
||||
'home': Home,
|
||||
'about': About,
|
||||
'expertise': Expertise,
|
||||
'portfolio': Portfolio,
|
||||
'project': Project,
|
||||
'jouer': Jouer,
|
||||
'game': Game,
|
||||
'blog': Blog,
|
||||
'article': Article,
|
||||
'default': Default
|
||||
home: Home,
|
||||
about: About,
|
||||
expertise: Expertise,
|
||||
portfolio: Portfolio,
|
||||
project: Project,
|
||||
jouer: Jouer,
|
||||
game: Game,
|
||||
blog: Blog,
|
||||
article: Article,
|
||||
default: Default
|
||||
}
|
||||
|
||||
const pageData = $derived(page.data)
|
||||
const template = $derived(pageData ? (page.template || 'default') : null)
|
||||
const View = $derived(template ? (templates[template] || Default) : null)
|
||||
const showFooter = $derived(template && template !== 'home')
|
||||
const wrapperWidth = $derived(`${slides.all.length * 100}vw`)
|
||||
const wrapperTransform = $derived(`translateX(-${slides.activeIndex * 100}vw)`)
|
||||
</script>
|
||||
|
||||
<div class="app">
|
||||
|
|
@ -40,14 +37,22 @@
|
|||
<Header />
|
||||
|
||||
<main class="main">
|
||||
{#if pageData && View}
|
||||
<View data={pageData} />
|
||||
<div
|
||||
class="slides-wrapper"
|
||||
style="width: {wrapperWidth}; transform: {wrapperTransform}"
|
||||
>
|
||||
{#each slides.all as slide}
|
||||
<div class="slide" data-slide={slide.id}>
|
||||
{#if slide.loaded}
|
||||
<svelte:component
|
||||
this={templates[slide.template] ?? Default}
|
||||
data={slide.data}
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</main>
|
||||
|
||||
{#if showFooter}
|
||||
<Footer />
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
|
|
@ -61,17 +66,7 @@
|
|||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
||||
background: #000;
|
||||
color: #fff;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
.app {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.main {
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
:global(a) {
|
||||
|
|
@ -83,4 +78,26 @@
|
|||
max-width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.app {
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
.main {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
.slides-wrapper {
|
||||
display: flex;
|
||||
height: 100%;
|
||||
transition: transform 1000ms cubic-bezier(0.77, 0, 0.175, 1);
|
||||
}
|
||||
|
||||
.slide {
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
</style>
|
||||
|
|
|
|||
|
|
@ -1,27 +1,20 @@
|
|||
<script>
|
||||
import { navigation } from '@state/navigation.svelte'
|
||||
import { locale } from '@state/locale.svelte'
|
||||
import { page } from '@state/page.svelte'
|
||||
import { navigateTo } from '@router'
|
||||
import { slides } from '@state/slides.svelte'
|
||||
import { slideTo } from '@router'
|
||||
|
||||
const isMenuOpen = $derived(navigation.isMenuOpen)
|
||||
const currentLang = $derived(locale.current)
|
||||
const currentPage = $derived(page.template || 'home')
|
||||
const activeId = $derived(slides.active?.id ?? 'home')
|
||||
const menuItems = $derived(slides.all.filter(s => s.id !== 'home'))
|
||||
|
||||
const translations = {
|
||||
expertise: { fr: 'EXPERTISE', en: 'EXPERTISE' },
|
||||
games: { fr: 'GAMES', en: 'GAMES' },
|
||||
play: { fr: 'PLAY', en: 'PLAY' },
|
||||
about: { fr: 'À PROPOS', en: 'ABOUT' },
|
||||
blog: { fr: 'BLOG', en: 'BLOG' }
|
||||
}
|
||||
|
||||
function t(key) {
|
||||
return translations[key]?.[currentLang] || translations[key]?.fr || key
|
||||
function getTitle(slide) {
|
||||
return slide.titles?.[currentLang] || slide.title || slide.id
|
||||
}
|
||||
|
||||
function handleNav(path) {
|
||||
navigateTo(path)
|
||||
slideTo(path)
|
||||
}
|
||||
|
||||
function toggleMenu() {
|
||||
|
|
@ -52,65 +45,19 @@
|
|||
/>
|
||||
</div>
|
||||
|
||||
{#each menuItems as slide}
|
||||
<div
|
||||
class="navbar-item clickable"
|
||||
class:active={currentPage === 'expertise'}
|
||||
class:active={activeId === slide.id}
|
||||
style="visibility: {isMenuOpen ? 'hidden' : 'visible'};"
|
||||
onclick={() => handleNav('/expertise')}
|
||||
onkeypress={(e) => e.key === 'Enter' && handleNav('/expertise')}
|
||||
onclick={() => handleNav(slide.path)}
|
||||
onkeypress={(e) => e.key === 'Enter' && handleNav(slide.path)}
|
||||
role="button"
|
||||
tabindex="0"
|
||||
>
|
||||
{t('expertise')}
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="navbar-item clickable"
|
||||
class:active={currentPage === 'portfolio'}
|
||||
style="visibility: {isMenuOpen ? 'hidden' : 'visible'};"
|
||||
onclick={() => handleNav('/portfolio')}
|
||||
onkeypress={(e) => e.key === 'Enter' && handleNav('/portfolio')}
|
||||
role="button"
|
||||
tabindex="0"
|
||||
>
|
||||
{t('games')}
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="navbar-item clickable"
|
||||
class:active={currentPage === 'jouer'}
|
||||
style="visibility: {isMenuOpen ? 'hidden' : 'visible'};"
|
||||
onclick={() => handleNav('/jouer')}
|
||||
onkeypress={(e) => e.key === 'Enter' && handleNav('/jouer')}
|
||||
role="button"
|
||||
tabindex="0"
|
||||
>
|
||||
{t('play')}
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="navbar-item clickable"
|
||||
class:active={currentPage === 'about'}
|
||||
style="visibility: {isMenuOpen ? 'hidden' : 'visible'};"
|
||||
onclick={() => handleNav('/a-propos')}
|
||||
onkeypress={(e) => e.key === 'Enter' && handleNav('/a-propos')}
|
||||
role="button"
|
||||
tabindex="0"
|
||||
>
|
||||
{t('about')}
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="navbar-item clickable"
|
||||
class:active={currentPage === 'blog' || currentPage === 'article'}
|
||||
style="visibility: {isMenuOpen ? 'hidden' : 'visible'};"
|
||||
onclick={() => handleNav('/blog')}
|
||||
onkeypress={(e) => e.key === 'Enter' && handleNav('/blog')}
|
||||
role="button"
|
||||
tabindex="0"
|
||||
>
|
||||
{t('blog')}
|
||||
{getTitle(slide)}
|
||||
</div>
|
||||
{/each}
|
||||
|
||||
<div
|
||||
class="clickable"
|
||||
|
|
|
|||
|
|
@ -1,93 +1,109 @@
|
|||
import navaid from "navaid";
|
||||
import { page } from "@state/page.svelte";
|
||||
import { navigation } from "@state/navigation.svelte";
|
||||
import { slides } from "@state/slides.svelte";
|
||||
import { site } from "@state/site.svelte";
|
||||
import { locale } from "@state/locale.svelte";
|
||||
|
||||
export const router = navaid("/", () => {
|
||||
// Default handler
|
||||
});
|
||||
let siteInitialized = false;
|
||||
|
||||
async function loadPage(path) {
|
||||
navigation.setLoading(true);
|
||||
page.setLoading(true);
|
||||
function normalizePath(path) {
|
||||
return path === "/" ? "/home" : path;
|
||||
}
|
||||
|
||||
async function loadSlide(path) {
|
||||
const idx = slides.getIndexByPath(path);
|
||||
if (idx !== -1) {
|
||||
const slide = slides.all[idx];
|
||||
if (slide.loaded || slide.loading) return;
|
||||
slides.setLoading(path, true);
|
||||
}
|
||||
|
||||
console.log(`[router] loadSlide: ${path}`);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${path}.json`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load page: ${response.status}`);
|
||||
}
|
||||
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.site) {
|
||||
console.log(`[router] fetch ok: ${path}`, {
|
||||
template: data.template,
|
||||
hasSite: !!data.site,
|
||||
navigation: data.site?.navigation,
|
||||
});
|
||||
|
||||
if (!siteInitialized && data.site) {
|
||||
site.set(data.site);
|
||||
locale.initialize(data.site.language, data.site.languages);
|
||||
slides.init(data.site.navigation);
|
||||
siteInitialized = true;
|
||||
console.log(`[router] slides initialized:`, slides.all.map(s => s.path));
|
||||
}
|
||||
|
||||
page.set({
|
||||
data,
|
||||
template: data.template || "default",
|
||||
url: path,
|
||||
loading: false,
|
||||
error: null,
|
||||
});
|
||||
|
||||
window.scrollTo(0, 0);
|
||||
slides.setData(path, data);
|
||||
console.log(`[router] setData done: ${path}, slides loaded:`, slides.all.map(s => `${s.path}=${s.loaded}`));
|
||||
} catch (error) {
|
||||
console.error("Failed to load page:", error);
|
||||
page.setError(error);
|
||||
} finally {
|
||||
navigation.setLoading(false);
|
||||
console.error(`[router] Failed to load slide ${path}:`, error);
|
||||
slides.setLoading(path, false);
|
||||
}
|
||||
}
|
||||
|
||||
// Route handlers
|
||||
router
|
||||
.on("/", () => loadPage("/home"))
|
||||
.on("/expertise", () => loadPage("/expertise"))
|
||||
.on("/portfolio", () => loadPage("/portfolio"))
|
||||
.on("/portfolio/:slug", ({ slug }) => loadPage(`/portfolio/${slug}`))
|
||||
.on("/jouer", () => loadPage("/jouer"))
|
||||
.on("/jouer/:slug", ({ slug }) => loadPage(`/jouer/${slug}`))
|
||||
.on("/a-propos", () => loadPage("/a-propos"))
|
||||
.on("/blog", () => loadPage("/blog"))
|
||||
.on("/blog/:slug", ({ slug }) => loadPage(`/blog/${slug}`))
|
||||
.on("*", (params) => {
|
||||
// Fallback for other routes
|
||||
loadPage(window.location.pathname);
|
||||
function loadAllSlidesInBackground(exceptPath) {
|
||||
slides.all
|
||||
.filter((s) => s.path !== exceptPath)
|
||||
.forEach((s) => loadSlide(s.path));
|
||||
}
|
||||
|
||||
export function slideTo(path, { skipHistory = false } = {}) {
|
||||
path = normalizePath(path);
|
||||
|
||||
if (!skipHistory) {
|
||||
history.pushState({}, "", path === "/home" ? "/" : path);
|
||||
}
|
||||
|
||||
const idx = slides.getIndexByPath(path);
|
||||
if (idx !== -1 && slides.all[idx].title) {
|
||||
document.title = `${slides.all[idx].title} — World Game`;
|
||||
}
|
||||
|
||||
slides.slideTo(path);
|
||||
|
||||
if (idx !== -1 && !slides.all[idx].loaded) {
|
||||
loadSlide(path);
|
||||
}
|
||||
}
|
||||
|
||||
export async function initRouter() {
|
||||
const initialPath = normalizePath(window.location.pathname);
|
||||
console.log(`[router] initRouter, initialPath: ${initialPath}`);
|
||||
|
||||
await loadSlide(initialPath);
|
||||
|
||||
const idx = slides.getIndexByPath(initialPath);
|
||||
console.log(`[router] after initial load, idx=${idx}, slides.all.length=${slides.all.length}`);
|
||||
if (idx !== -1) {
|
||||
slides.setActiveIndex(idx);
|
||||
}
|
||||
|
||||
loadAllSlidesInBackground(initialPath);
|
||||
|
||||
window.addEventListener("popstate", () => {
|
||||
const path = normalizePath(window.location.pathname);
|
||||
slideTo(path, { skipHistory: true });
|
||||
});
|
||||
|
||||
export function initRouter() {
|
||||
// Start listening to route changes
|
||||
router.listen();
|
||||
|
||||
// Intercept internal link clicks
|
||||
document.addEventListener("click", (e) => {
|
||||
const link = e.target.closest("a");
|
||||
if (!link) return;
|
||||
|
||||
const url = new URL(link.href, window.location.origin);
|
||||
|
||||
// Only intercept same-origin links without target attribute
|
||||
if (
|
||||
url.origin === window.location.origin &&
|
||||
!link.target &&
|
||||
!link.hasAttribute("download")
|
||||
) {
|
||||
e.preventDefault();
|
||||
navigateTo(url.pathname);
|
||||
slideTo(url.pathname);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle browser back/forward
|
||||
window.addEventListener("popstate", () => {
|
||||
router.run(window.location.pathname);
|
||||
});
|
||||
}
|
||||
|
||||
export function navigateTo(path) {
|
||||
window.history.pushState({}, "", path);
|
||||
loadPage(path);
|
||||
}
|
||||
// Keep navigateTo as alias so existing views don't break
|
||||
export const navigateTo = slideTo;
|
||||
|
|
|
|||
76
src/state/slides.svelte.js
Normal file
76
src/state/slides.svelte.js
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
let slidesData = $state([])
|
||||
let activeIndex = $state(0)
|
||||
let pendingPath = $state(null)
|
||||
|
||||
function getIndexByPath(path) {
|
||||
return slidesData.findIndex(s => s.path === path)
|
||||
}
|
||||
|
||||
export const slides = {
|
||||
get all() { return slidesData },
|
||||
get activeIndex() { return activeIndex },
|
||||
get active() { return slidesData[activeIndex] ?? null },
|
||||
get pendingPath() { return pendingPath },
|
||||
|
||||
init(siteNavigation) {
|
||||
slidesData = siteNavigation.map(nav => ({
|
||||
id: nav.id,
|
||||
path: nav.url,
|
||||
title: nav.title,
|
||||
titles: nav.titles || { fr: nav.title },
|
||||
template: nav.id,
|
||||
data: null,
|
||||
loaded: false,
|
||||
loading: false
|
||||
}))
|
||||
},
|
||||
|
||||
setData(path, data) {
|
||||
const idx = getIndexByPath(path)
|
||||
if (idx === -1) return
|
||||
slidesData[idx] = {
|
||||
...slidesData[idx],
|
||||
template: data.template || slidesData[idx].id,
|
||||
data,
|
||||
loaded: true,
|
||||
loading: false
|
||||
}
|
||||
if (pendingPath === path) {
|
||||
slides.resolvePending()
|
||||
}
|
||||
},
|
||||
|
||||
setLoading(path, bool) {
|
||||
const idx = getIndexByPath(path)
|
||||
if (idx === -1) return
|
||||
slidesData[idx] = { ...slidesData[idx], loading: bool }
|
||||
},
|
||||
|
||||
slideTo(path) {
|
||||
const idx = getIndexByPath(path)
|
||||
if (idx === -1) return
|
||||
if (slidesData[idx].loaded) {
|
||||
activeIndex = idx
|
||||
pendingPath = null
|
||||
} else {
|
||||
pendingPath = path
|
||||
}
|
||||
},
|
||||
|
||||
setActiveIndex(idx) {
|
||||
activeIndex = idx
|
||||
},
|
||||
|
||||
resolvePending() {
|
||||
if (!pendingPath) return
|
||||
const idx = getIndexByPath(pendingPath)
|
||||
if (idx !== -1 && slidesData[idx].loaded) {
|
||||
activeIndex = idx
|
||||
pendingPath = null
|
||||
}
|
||||
},
|
||||
|
||||
getIndexByPath(path) {
|
||||
return getIndexByPath(path)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue