Skip to content
Merged
52 changes: 42 additions & 10 deletions docs/_includes/default.njk
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,46 @@

</head>
<body>
{# Menu trigger #}
<a class="menu-trigger text-muted" href="#nav">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon">
<path d="M4 6l16 0" />
<path d="M4 12l16 0" />
<path d="M4 18l16 0" />
</svg>
</a>
<header>
{# Menu trigger #}
<a class="menu-trigger text-muted" href="#nav">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon">
<path d="M4 6l16 0" />
<path d="M4 12l16 0" />
<path d="M4 18l16 0" />
</svg>
</a>

<div class="controls">
<div>
<a class="button hollow neutral rounded-full" href="#search" onclick="focusSearch()">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon"><path d="M10 10m-7 0a7 7 0 1 0 14 0a7 7 0 1 0 -14 0" /><path d="M21 21l-6 -6" /></svg>
Search
</a>
</div>
<div class="theme-mode">
<button id="light-mode" onclick="setLight()" class="ghost neutral rounded-full">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon"><path d="M12 12m-4 0a4 4 0 1 0 8 0a4 4 0 1 0 -8 0" /><path d="M3 12h1m8 -9v1m8 8h1m-9 8v1m-6.4 -15.4l.7 .7m12.1 -.7l-.7 .7m0 11.4l.7 .7m-12.1 -.7l-.7 .7" /></svg>
</button>
<button id="dark-mode" onclick="setDark()" class="ghost neutral rounded-full">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon"><path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" /></svg>
</button>
</div>
</div>
</header>

{# Search result #}
<div id="search">
<input type="search" placeholder="Search" autofocus />
<hr />
<div id="result">
<div class="state empty">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon"><path d="M12 3a3 3 0 0 0 -3 3v12a3 3 0 0 0 3 3" /><path d="M6 3a3 3 0 0 1 3 3v12a3 3 0 0 1 -3 3" /><path d="M13 7h7a1 1 0 0 1 1 1v8a1 1 0 0 1 -1 1h-7" /><path d="M5 7h-1a1 1 0 0 0 -1 1v8a1 1 0 0 0 1 1h1" /><path d="M17 12h.01" /><path d="M13 12h.01" /></svg>
<h4>Waiting for search input</h4>
<p class="text-muted">Please type something into the search field.</p>
</div>
</div>
</div>

{# Global navigation #}
<aside id="nav">
Expand All @@ -47,11 +79,11 @@
</div>
<div class="actions">
<a href="{{ repoUrl }}" target="_blank" class="button hollow neutral sm">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M9 19c-4.3 1.4 -4.3 -2.5 -6 -3m12 5v-3.5c0 -1 .1 -1.4 -.5 -2c2.8 -.3 5.5 -1.4 5.5 -6a4.6 4.6 0 0 0 -1.3 -3.2a4.2 4.2 0 0 0 -.1 -3.2s-1.1 -.3 -3.5 1.3a12.3 12.3 0 0 0 -6.2 0c-2.4 -1.6 -3.5 -1.3 -3.5 -1.3a4.2 4.2 0 0 0 -.1 3.2a4.6 4.6 0 0 0 -1.3 3.2c0 4.6 2.7 5.7 5.5 6c-.6 .6 -.6 1.2 -.5 2v3.5" /></svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon"><path d="M9 19c-4.3 1.4 -4.3 -2.5 -6 -3m12 5v-3.5c0 -1 .1 -1.4 -.5 -2c2.8 -.3 5.5 -1.4 5.5 -6a4.6 4.6 0 0 0 -1.3 -3.2a4.2 4.2 0 0 0 -.1 -3.2s-1.1 -.3 -3.5 1.3a12.3 12.3 0 0 0 -6.2 0c-2.4 -1.6 -3.5 -1.3 -3.5 -1.3a4.2 4.2 0 0 0 -.1 3.2a4.6 4.6 0 0 0 -1.3 3.2c0 4.6 2.7 5.7 5.5 6c-.6 .6 -.6 1.2 -.5 2v3.5" /></svg>
GitHub
</a>
<a href="{{ donationUrl }}" target="_blank" class="button hollow sm">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M19.5 12.572l-7.5 7.428l-7.5 -7.428a5 5 0 1 1 7.5 -6.566a5 5 0 1 1 7.5 6.572" /></svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon"><path d="M19.5 12.572l-7.5 7.428l-7.5 -7.428a5 5 0 1 1 7.5 -6.566a5 5 0 1 1 7.5 6.572" /></svg>
Support
</a>
</div>
Expand Down
83 changes: 83 additions & 0 deletions docs/assets/scripts/docs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,73 @@
// Theming
const lightToggle = document.querySelector('#light-mode');
const darkToggle = document.querySelector('#dark-mode');
const setDark = () => {
localStorage.setItem('sloth.css/theme', 'dark');
document.documentElement.style.colorScheme = 'dark';
darkToggle.classList.add(['text-accent']);
lightToggle.classList.remove(['text-accent']);
};
const setLight = () => {
localStorage.setItem('sloth.css/theme', 'light');
document.documentElement.style.colorScheme = 'light';
lightToggle.classList.add(['text-accent']);
darkToggle.classList.remove(['text-accent']);
};

// Search
let index = [];
fetch('/assets/scripts/searchIndex.json')
.then((response) => response.json())
.then((data) => { index = data; });

const search = document.querySelector('#search');
const searchInput = search.querySelector('input');
const searchResult = search.querySelector('#result');
searchInput.addEventListener('input', (event) => {
const query = event.target.value.toLowerCase();

// Handle empty input
if (!query.length) {
searchResult.innerHTML = `<div class="state empty">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon"><path d="M12 3a3 3 0 0 0 -3 3v12a3 3 0 0 0 3 3" /><path d="M6 3a3 3 0 0 1 3 3v12a3 3 0 0 1 -3 3" /><path d="M13 7h7a1 1 0 0 1 1 1v8a1 1 0 0 1 -1 1h-7" /><path d="M5 7h-1a1 1 0 0 0 -1 1v8a1 1 0 0 0 1 1h1" /><path d="M17 12h.01" /><path d="M13 12h.01" /></svg>
<h4>Waiting for search input</h4>
<p class="text-muted">Please type something into the search field.</p>
</div>`;
return;
}

// Calculate search result
const result = index.filter((e) => (`${e.title} ${e.text} ${e.tags}`).toLowerCase().includes(query));

// Handle search results
if (result.length) {
searchResult.innerHTML = result.reduce((p, c) => {
const icon = {
'getting-started': '<path d="M4 13a8 8 0 0 1 7 7a6 6 0 0 0 3 -5a9 9 0 0 0 6 -8a3 3 0 0 0 -3 -3a9 9 0 0 0 -8 6a6 6 0 0 0 -5 3" /><path d="M7 14a6 6 0 0 0 -3 6a6 6 0 0 0 6 -3" /><path d="M15 9m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />',
'core': '<path d="M6 17.6l-2 -1.1v-2.5" /><path d="M4 10v-2.5l2 -1.1" /><path d="M10 4.1l2 -1.1l2 1.1" /><path d="M18 6.4l2 1.1v2.5" /><path d="M20 14v2.5l-2 1.12" /><path d="M14 19.9l-2 1.1l-2 -1.1" /><path d="M12 12l2 -1.1" /><path d="M18 8.6l2 -1.1" /><path d="M12 12l0 2.5" /><path d="M12 18.5l0 2.5" /><path d="M12 12l-2 -1.12" /><path d="M6 8.6l-2 -1.1" />',
'utilities': '<path d="M7 10h3v-3l-3.5 -3.5a6 6 0 0 1 8 8l6 6a2 2 0 0 1 -3 3l-6 -6a6 6 0 0 1 -8 -8l3.5 3.5" />',
'components': '<path d="M3.604 7.197l7.138 -3.109a.96 .96 0 0 1 1.27 .527l4.924 11.902a1 1 0 0 1 -.514 1.304l-7.137 3.109a.96 .96 0 0 1 -1.271 -.527l-4.924 -11.903a1 1 0 0 1 .514 -1.304z" /><path d="M15 4h1a1 1 0 0 1 1 1v3.5" /><path d="M20 6c.264 .112 .52 .217 .768 .315a1 1 0 0 1 .53 1.311l-2.298 5.374" />',
}[c.cat];
return `${p}<a href="${c.url}" class="entry">
<div class="text-muted">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon">${icon}</svg>
</div>
<div>
<h5>${c.name}</h5>
<div class="text-muted">${c.text}</div>
</div>
</a>`;
}, '')
} else {
searchResult.innerHTML = `<div class="state empty">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="icon"><path d="M3 7v4a1 1 0 0 0 1 1h3" /><path d="M7 7v10" /><path d="M10 8v8a1 1 0 0 0 1 1h2a1 1 0 0 0 1 -1v-8a1 1 0 0 0 -1 -1h-2a1 1 0 0 0 -1 1z" /><path d="M17 7v4a1 1 0 0 0 1 1h3" /><path d="M21 7v10" /></svg>
<h4>Sorry, no matching pages found</h4>
<p class="text-muted">Please try changing the search term.</p>
</div>`;
}
});
const focusSearch = () => setTimeout(() => searchInput.focus(), 50);

// Component: Toast
const toast = document.querySelector('#toast');
const toggleToast = () => toast.classList.toggle('active');
Expand All @@ -11,3 +81,16 @@ const colorToast = (col) => {
toastColors.forEach(c => toast.classList.remove(c));
toast.classList.add(col);
}

// Ready? Let's go!
(() => {
// Handle initial theme mode
if (localStorage.getItem('sloth.css/theme') === 'dark') {
document.documentElement.style.colorScheme = 'dark';
darkToggle.classList.add('text-accent');
}
if (localStorage.getItem('sloth.css/theme') === 'light') {
document.documentElement.style.colorScheme = 'light';
lightToggle.classList.add('text-accent');
}
})();
Loading