-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
16 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"astro-vtbot": patch | ||
--- | ||
|
||
Adds a `production` property to the `<BrakePad />`. Making the brake a pure DEV mode features unless requested otherwise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,32 @@ | ||
--- | ||
export interface Props { | ||
duration?: number; | ||
production?: boolean; | ||
} | ||
const TAG = 'vtbot-brake-pad'; // see also start of script | ||
const { duration = 2000 } = Astro.props; | ||
if (!import.meta.env.DEV) { | ||
console.error( | ||
`${Astro.url.pathname}: Active brake pad in production? Don't forget to remove it!` | ||
); | ||
let { duration = 2000 } = Astro.props; | ||
if (!(import.meta.env.DEV || Astro.props.production)) { | ||
duration = 0; | ||
} | ||
--- | ||
|
||
<meta name={TAG} content={'' + duration} /> | ||
{duration > 0 && <meta name={TAG} content={'' + duration} />} | ||
|
||
<script> | ||
{ duration > 0 && <script type="module"> | ||
const TAG = 'vtbot-brake-pad'; | ||
|
||
import { | ||
TRANSITION_BEFORE_PREPARATION, | ||
isTransitionBeforePreparationEvent, | ||
} from 'astro:transitions/client'; | ||
|
||
const duration = () => | ||
(document.querySelector(`meta[name="${TAG}"]`) as HTMLMetaElement)?.content; | ||
(document.querySelector(`meta[name="${TAG}"]`))?.content; | ||
|
||
const beforePreparation = (event: Event) => { | ||
const beforePreparation = (event) => { | ||
const waitTime = duration(); | ||
|
||
if (waitTime && isTransitionBeforePreparationEvent(event)) { | ||
if (waitTime) { | ||
const originalLoader = event.loader; | ||
event.loader = async () => { | ||
await originalLoader(); | ||
await new Promise((resolve) => setTimeout(resolve, parseInt(waitTime, 10))); | ||
}; | ||
} | ||
}; | ||
document.addEventListener(TRANSITION_BEFORE_PREPARATION, beforePreparation); | ||
</script> | ||
document.addEventListener('astro:before-preparation', beforePreparation); | ||
</script>} |