Fix JavaScript not working after switching pages

This commit is contained in:
Martin Prokoph
2024-09-18 12:58:35 +02:00
parent f102516b76
commit b2e20fa63b
5 changed files with 637 additions and 1289 deletions

1699
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -11,24 +11,24 @@
"preview": "astro preview" "preview": "astro preview"
}, },
"devDependencies": { "devDependencies": {
"@astrojs/mdx": "^2.3.1", "@astrojs/mdx": "^3.1.6",
"@astrojs/partytown": "^2.1.2", "@astrojs/partytown": "^2.1.2",
"@astrojs/tailwind": "^5.1.1", "@astrojs/tailwind": "^5.1.1",
"@iconify-json/ion": "^1.2.0", "@iconify-json/ion": "^1.2.0",
"@iconify-json/mdi": "^1.2.0", "@iconify-json/mdi": "^1.2.0",
"@iconify-json/simple-icons": "^1.2.3", "@iconify-json/simple-icons": "^1.2.3",
"@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/eslint-plugin": "^8.6.0",
"@typescript-eslint/parser": "^5.62.0", "@typescript-eslint/parser": "^8.6.0",
"accessible-astro-components": "^2.3.6", "accessible-astro-components": "^2.3.6",
"astro": "^4.15.7", "astro": "^4.15.7",
"astro-compress": "^2.3.1", "astro-compress": "^2.3.1",
"astro-icon": "^1.1.1", "astro-icon": "^1.1.1",
"eslint": "^8.57.1", "eslint": "^9.10.0",
"eslint-plugin-astro": "^0.23.0", "eslint-plugin-astro": "^1.2.4",
"eslint-plugin-jsx-a11y": "^6.10.0", "eslint-plugin-jsx-a11y": "^6.10.0",
"prettier": "^2.8.8", "prettier": "^3.3.3",
"prettier-plugin-astro": "^0.8.1", "prettier-plugin-astro": "^0.14.1",
"prettier-plugin-tailwindcss": "^0.2.8", "prettier-plugin-tailwindcss": "^0.6.6",
"sass": "^1.79.1", "sass": "^1.79.1",
"svgo": "^3.3.2", "svgo": "^3.3.2",
"tailwindcss": "^3.4.12" "tailwindcss": "^3.4.12"

View File

@@ -5,7 +5,7 @@ interface Props {
const { includeHashtag } = Astro.props const { includeHashtag } = Astro.props
import { Icon } from 'astro-icon/components' import { Icon } from 'astro-icon/components'
--- ---
<color-picker>
<section class="my-6"> <section class="my-6">
<div class=""> <div class="">
<p class="text-lg">Select a color below to get the hex code:</p> <p class="text-lg">Select a color below to get the hex code:</p>
@@ -26,8 +26,12 @@ import { Icon } from 'astro-icon/components'
</div> </div>
</div> </div>
</section> </section>
</color-picker>
<script> <script>
// Define the behaviour for our new type of HTML element.
class ColorPicker extends HTMLElement {
connectedCallback() {
// Get the 2 elements from above // Get the 2 elements from above
const colorLabel = document.getElementById("color-label"); const colorLabel = document.getElementById("color-label");
const colorSelect = document.getElementById("color-select"); const colorSelect = document.getElementById("color-select");
@@ -46,7 +50,6 @@ import { Icon } from 'astro-icon/components'
// Add event listener to copy the color to the clipboard // Add event listener to copy the color to the clipboard
if (colorLabel) copyButton?.addEventListener('click', () => copyString(colorLabel.innerText, copyButton)); if (colorLabel) copyButton?.addEventListener('click', () => copyString(colorLabel.innerText, copyButton));
async function copyString(text: string, button: HTMLElement) { async function copyString(text: string, button: HTMLElement) {
await navigator.clipboard.writeText(text); await navigator.clipboard.writeText(text);
@@ -67,5 +70,13 @@ import { Icon } from 'astro-icon/components'
if (successIcon) successIcon.style.display = 'none'; if (successIcon) successIcon.style.display = 'none';
}, 1000); }, 1000);
} }
}
}
// Tell the browser to use our ColorPicker class for <color-picker> elements.
customElements.define('color-picker', ColorPicker);
</script> </script>

View File

@@ -3,8 +3,22 @@ import DefaultLayout from './DefaultLayout.astro'
const { frontmatter } = Astro.props const { frontmatter } = Astro.props
--- ---
<DefaultLayout title={frontmatter.title}>
<div class="container">
<div class="space-content my-12">
<slot />
</div>
</div>
</DefaultLayout>
<!-- Copy Code Buttons based on: https://timneubauer.dev/blog/copy-code-button-in-astro/ --> <!-- Copy Code Buttons based on: https://timneubauer.dev/blog/copy-code-button-in-astro/ -->
<script> <script>
initCopyCodeButtons();
document.addEventListener('astro:after-swap', initCopyCodeButtons);
function initCopyCodeButtons() {
console.log("Initializing Copy Code Buttons...");
let copyButtonLabel = "Copy Code"; let copyButtonLabel = "Copy Code";
let codeBlocks = Array.from(document.querySelectorAll("pre")); let codeBlocks = Array.from(document.querySelectorAll("pre"));
@@ -40,16 +54,9 @@ const { frontmatter } = Astro.props
button.innerText = copyButtonLabel; button.innerText = copyButtonLabel;
}, 700); }, 700);
} }
}
</script> </script>
<DefaultLayout title={frontmatter.title}>
<div class="container">
<div class="space-content my-12">
<slot />
</div>
</div>
</DefaultLayout>
<style is:global> <style is:global>
.copy-code { .copy-code {
position: absolute; position: absolute;

View File

@@ -174,12 +174,3 @@ public ConfigScreenFactory<?> getModConfigScreenFactory() {
return parent -> MidnightConfig.getScreen(parent, "modid"); return parent -> MidnightConfig.getScreen(parent, "modid");
}` }`
``` ```
<script>
import Alpine from "alpinejs";
document.addEventListener('alpine:init', () => {
//Alpine.data('versionSelection', () => ({
//}))
})
</script>