Skip to content

Instantly share code, notes, and snippets.

View SimeonGriggs's full-sized avatar

Simeon Griggs SimeonGriggs

View GitHub Profile
@SimeonGriggs
SimeonGriggs / useTailwindBreakpoint.js
Created January 4, 2021 20:50
React Hook which returns the current TailwindCSS breakpoint size based on the current window width.
import { useState, useEffect } from 'react'
import resolveConfig from 'tailwindcss/resolveConfig'
import throttle from 'lodash.throttle'
import tailwindConfig from '../tailwind.config.js'
const findKeyByValue = (object, value) =>
Object.keys(object).find((key) => object[key] === value)
const getDeviceConfig = (width) => {
const fullConfig = resolveConfig(tailwindConfig)
/**
* ---------------------------------------------------------------------------------
* This file has been generated by Sanity TypeGen.
* Command: `sanity typegen generate`
*
* Any modifications made directly to this file will be overwritten the next time
* the TypeScript definitions are generated. Please make changes to the Sanity
* schema definitions and/or GROQ queries if you need to update these types.
*
* For more information on how to use Sanity TypeGen, visit the official documentation:
@SimeonGriggs
SimeonGriggs / PageBuilder.tsx
Last active March 19, 2024 12:54
Rendering blocks from a "page builder" array in Sanity
import type { KeyedObject, TypedObject } from "sanity";
import PageBuilderContent from "./pageBuilderContent";
import PageBuilderColumns from "./pageBuilderColumns";
const Components = {
pageBuilderContent: PageBuilderContent,
pageBuilderColumns: PageBuilderColumns,
};
@SimeonGriggs
SimeonGriggs / slugWithType.js
Created March 22, 2021 18:03
Sanity.io: Extend the built-in `slug` field type with validation rules and slug prefixes
import slugify from 'slugify'
/**
* Slug field which will append a 'prefix' so that your slug is the complete path to the file
* And so that retrieving the slug from a reference to a document is a ready-made link
*
* Example with no prefix:
* `office` becomes `/office`
*
* Example with `office` prefix:
@SimeonGriggs
SimeonGriggs / preview.ts
Last active December 23, 2023 23:28
Sanity + Next.js Preview API Route with Config for SEO Pane
// ./pages/api/preview.ts
import { groq } from "next-sanity";
import { previewClient } from "../../lib/sanity.client";
import { NextApiRequest, NextApiResponse } from "next";
export const STUDIO_URL_DEV = "http://localhost:3333";
export const STUDIO_URL_PROD = "https://replace-with-yours.sanity.studio";
export const WEBSITE_URL_DEV = "http://localhost:3000";
@SimeonGriggs
SimeonGriggs / ImageExtendedInput.tsx
Created September 11, 2023 17:16
An extended image asset schema with a custom input to watch the value and update the image's fields with values from the asset itself
import {useEffect, useState} from 'react'
import {ImageAsset, ObjectInputProps, Reference, set, unset, useClient} from 'sanity'
type ExtendedImageValue = {
asset: Reference
lqip?: string
blurHash?: string
}
export default function ImageExtendedInput(props: ObjectInputProps<ExtendedImageValue>) {
@SimeonGriggs
SimeonGriggs / postType.ts
Last active July 14, 2023 14:42
Sanity.io: A slug field with a prefix based on a "category" reference's slug
import {SlugSourceFn, SlugifierFn, defineField, defineType} from 'sanity'
import get from 'lodash/get'
// Fetch the category's slug – if present – and append the post's title
const handleSlugSource: SlugSourceFn = async (doc, context) => {
const categoryRef = get(doc, `category._ref`)
if (categoryRef) {
const client = context.getClient({apiVersion: `2023-07-01`})
const categorySlug = await client.fetch(`*[_id == $id][0].slug.current`, {id: categoryRef})
@SimeonGriggs
SimeonGriggs / desk-structure.js
Last active February 7, 2023 20:33
Sanity.io – Customize Desk Structure based on User Roles
import S from '@sanity/desk-tool/structure-builder'
import userStore from 'part:@sanity/base/user'
// Get the logged in user
const getCurrentUser = () => {
userStore.me.subscribe((user) => {
// Instead of a local variable, we use this window object to re-use it through the Studio
if (user) {
window._sanityUser = user ?? undefined
}
@SimeonGriggs
SimeonGriggs / Dereference.tsx
Created December 16, 2022 15:42
Sanity Studio v3 Document Action to "de-reference" a document by patching all documents that reference the current one. Incredibly dangerous.
import {useState, useEffect, useCallback} from 'react'
import {useToast, Card, Button, Stack, Text, Code} from '@sanity/ui'
import {extractWithPath} from '@sanity/mutator'
import {
Preview,
DocumentActionProps,
SanityDocument,
useClient,
useSchema,
pathToString,
@SimeonGriggs
SimeonGriggs / useListeningQuery.ts
Last active November 30, 2022 05:59
A custom hook for making and listening to document changes
import React, {useEffect, useState, useRef} from 'react'
import documentStore from 'part:@sanity/base/datastore/document'
import {catchError, distinctUntilChanged} from 'rxjs/operators'
import isEqual from 'react-fast-compare'
type Params = Record<string, string | number | boolean | string[]>
interface ListenQueryOptions {
tag?: string
apiVersion?: string