Skip to content

Instantly share code, notes, and snippets.

@codeflorist
codeflorist / useBreakpoints.ts
Created January 25, 2024 17:04
Nuxt 3 composable providing reactive variables regarding current breakpoint status.
/*
Nuxt 3 composable providing reactive variables
regarding current breakpoint status.
---------------------------------------------
Usage:
const { current, min, max } = useBreakpoints()
// returns current breakpoint
console.log(current.value) // e.g. 'md'
@codeflorist
codeflorist / useStoryRoutes.ts
Last active March 28, 2024 13:19
Multilingual route management for using Nuxt3 with Storyblok CMS
export const useStoryRoutes = () => {
type StoryRoute = {
storyPath: string // The backend-storyblok-path of the story.
uuid: string // The uuid of the story.
localeTitles: LocalizedText
localePaths: LocalizedText
}
const pageTypes = 'page,news-article'
@codeflorist
codeflorist / storyblok-v1-image-provider.ts
Last active October 12, 2023 08:24
Custom nuxt/image provider for the Storyblok Image Service v1
/*
Provider to use the old storyblok image service v1
with the nuxt/image package.
Why use v1 instead of the new v2?
Because v2 has serious problems with CDN caching,
with cloudfront CDN cache invalidating every day
instead of 1 year. This leads to response times
of up to multiple seconds due to re-generation
of the image service. Sadly, Storyblok has not yet
@codeflorist
codeflorist / nl2misc.ts
Last active March 17, 2023 10:19
Nuxt 3 Plugin providing directives and helpers to convert line breaks to <br>, <p> or arrays.
/*
Nuxt 3 Plugin providing directives and helpers
to convert line breaks to <br>, <p> or arrays.
---------------------------------------------
Usage as directives (WARNING: can lead to XSS attack!):
<div v-nl2br="string" />
<div v-nl2p="string" />
Usage as helper in script setup:
const { $nl2array, $nl2br, $nl2p } = useNuxtApp()
@codeflorist
codeflorist / isBot.client.ts
Last active March 17, 2023 09:59
Nuxt 3 Plugin to detect bots
/*
Nuxt 3 Plugin to detect bots
---------------------------------------------
Usage as helper in script setup:
const { $isBot } = useNuxtApp()
const isBot = $isBot()
Usage as helper in template:
<div v-if="$isBot()"></div>
@codeflorist
codeflorist / convertCase.ts
Last active March 17, 2023 10:20
Nuxt 3 Plugin providing helpers to convert anything to kebap-case, snake_case, camelCase or PascalCase
/*
Nuxt 3 Plugin providing helpers to convert anything
to kebap-case, snake_case, camelCase or PascalCase.
---------------------------------------------------
Usage as helper in script setup:
const {$toKebabCase, $toSnakeCase, $toCamelCase, $toPascalCase} = useNuxtApp()
console.log($toKebabCase('My String')) // Outputs: my-string
console.log($toSnakeCase('My String')) // Outputs: my_string
console.log($toCamelCase('My String')) // Outputs: myString