Skip to content

Instantly share code, notes, and snippets.

@PezzerDev
PezzerDev / log-error.ts
Created May 3, 2024 02:41
Typescript error logger #typescript
export function logError(err: unknown, context?: string) {
// Convert to Error if it's not one
let error: Error;
if (err instanceof Error) {
error = err;
} else {
// Wrap string in Error, but pop first line of stack (which will be this line)
error = new Error(String(err));
error.stack = error.stack?.split('\n').slice(1).join('\n');
}
@PezzerDev
PezzerDev / .editorconifig
Created May 3, 2024 23:19
.editorconfig template #format #template
root = true
[*]
end_of_line = lf
insert_final_newline = true
indent_style = tab
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
@PezzerDev
PezzerDev / fouc-off.svelte
Last active May 7, 2024 13:43
FOUC Off (Svelte) #svelte #fouc
<!-- This component disables CSS transitions until onMount fires, in order to avoid FOUC -->
<script lang="ts">
import { tick } from 'svelte';
// Adds 'mounted' class to body to signal that transitions should be enabled
$effect(() => {
tick().then(() => document.firstElementChild?.setAttribute('data-mounted', 'true'));
});
</script>
@PezzerDev
PezzerDev / blur-on-escape.ts
Last active May 6, 2024 21:18
Blur on Escape directive #svelte #directive #typescript
export interface Options {
bubble: boolean;
}
// When applied to an input, it will blur the input when the escape key is pressed
// By default it will prevent the default action of the escape key, which is useful
// for inputs in dialogs
export default function blurOnEscape(node: HTMLElement, options: Options = { bubble: false }) {
const onKeydown = (event: KeyboardEvent) => {
if (event.key !== 'Escape') return;