UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

1 lines 4.44 kB
{"version":3,"file":"useMatch.cjs","names":["Solid","invariant","replaceEqualDeep","nearestMatchContext","useRouter","AnyRouter","MakeRouteMatch","MakeRouteMatchUnion","RegisteredRouter","StrictOrFrom","ThrowConstraint","ThrowOrOptional","UseMatchBaseOptions","select","match","TRouter","TFrom","TStrict","TSelected","shouldThrow","TThrow","UseMatchRoute","opts","Accessor","UseMatchResult","UseMatchOptions","useMatch","router","contextMatch","useContext","nearestMatch","from","undefined","stores","getMatchStore","get","createEffect","process","env","NODE_ENV","Error","createMemo","prev","selectedMatch","res"],"sources":["../../src/useMatch.tsx"],"sourcesContent":["import * as Solid from 'solid-js'\nimport { invariant, replaceEqualDeep } from '@tanstack/router-core'\nimport { nearestMatchContext } from './matchContext'\nimport { useRouter } from './useRouter'\nimport type {\n AnyRouter,\n MakeRouteMatch,\n MakeRouteMatchUnion,\n RegisteredRouter,\n StrictOrFrom,\n ThrowConstraint,\n ThrowOrOptional,\n} from '@tanstack/router-core'\n\nexport interface UseMatchBaseOptions<\n TRouter extends AnyRouter,\n TFrom,\n TStrict extends boolean,\n TThrow extends boolean,\n TSelected,\n> {\n select?: (\n match: MakeRouteMatch<TRouter['routeTree'], TFrom, TStrict>,\n ) => TSelected\n shouldThrow?: TThrow\n}\n\nexport type UseMatchRoute<out TFrom> = <\n TRouter extends AnyRouter = RegisteredRouter,\n TSelected = unknown,\n>(\n opts?: UseMatchBaseOptions<TRouter, TFrom, true, true, TSelected>,\n) => Solid.Accessor<UseMatchResult<TRouter, TFrom, true, TSelected>>\n\nexport type UseMatchOptions<\n TRouter extends AnyRouter,\n TFrom extends string | undefined,\n TStrict extends boolean,\n TThrow extends boolean,\n TSelected,\n> = StrictOrFrom<TRouter, TFrom, TStrict> &\n UseMatchBaseOptions<TRouter, TFrom, TStrict, TThrow, TSelected>\n\nexport type UseMatchResult<\n TRouter extends AnyRouter,\n TFrom,\n TStrict extends boolean,\n TSelected,\n> = unknown extends TSelected\n ? TStrict extends true\n ? MakeRouteMatch<TRouter['routeTree'], TFrom, TStrict>\n : MakeRouteMatchUnion<TRouter>\n : TSelected\n\nexport function useMatch<\n TRouter extends AnyRouter = RegisteredRouter,\n const TFrom extends string | undefined = undefined,\n TStrict extends boolean = true,\n TThrow extends boolean = true,\n TSelected = unknown,\n>(\n opts: UseMatchOptions<\n TRouter,\n TFrom,\n TStrict,\n ThrowConstraint<TStrict, TThrow>,\n TSelected\n >,\n): Solid.Accessor<\n ThrowOrOptional<UseMatchResult<TRouter, TFrom, TStrict, TSelected>, TThrow>\n> {\n const router = useRouter<TRouter>()\n const contextMatch = Solid.useContext(nearestMatchContext)\n const nearestMatch = opts.from ? undefined : contextMatch\n\n const match = () => {\n if (opts.from) {\n return router.stores.getMatchStore(opts.from).get()\n }\n\n return nearestMatch?.[1 /* match */]()\n }\n\n Solid.createEffect(() => {\n if (match() !== undefined) {\n return\n }\n\n if (opts.shouldThrow ?? true) {\n if (process.env.NODE_ENV !== 'production') {\n throw new Error(\n `Invariant failed: Could not find ${opts.from ? `an active match from \"${opts.from}\"` : 'a nearest match!'}`,\n )\n }\n\n invariant()\n }\n })\n\n return Solid.createMemo((prev: TSelected | undefined) => {\n const selectedMatch = match()\n\n if (selectedMatch === undefined) {\n return undefined\n }\n\n const res = opts.select ? opts.select(selectedMatch) : selectedMatch\n if (prev === undefined) return res as TSelected\n return replaceEqualDeep(prev, res) as TSelected\n }) as any\n}\n"],"mappings":";;;;;;;AAsDA,SAAgB0B,SAOdJ,MASA;CACA,MAAMK,SAASvB,kBAAAA,UAAmB;CAClC,MAAMwB,eAAe5B,SAAM6B,WAAW1B,qBAAAA,mBAAmB;CACzD,MAAM2B,eAAeR,KAAKS,OAAOC,KAAAA,IAAYJ;CAE7C,MAAMd,cAAc;EAClB,IAAIQ,KAAKS,MACP,OAAOJ,OAAOM,OAAOC,cAAcZ,KAAKS,IAAI,EAAEI,IAAI;EAGpD,OAAOL,eAAe,GAAe;CACvC;CAEA9B,SAAMoC,mBAAmB;EACvB,IAAItB,MAAM,MAAMkB,KAAAA,GACd;EAGF,IAAIV,KAAKH,eAAe,MAAM;GAC5B,IAAA,QAAA,IAAA,aAA6B,cAC3B,MAAM,IAAIqB,MACR,oCAAoClB,KAAKS,OAAO,yBAAyBT,KAAKS,KAAI,KAAM,oBAC1F;GAGF9B,CAAAA,GAAAA,sBAAAA,WAAU;EACZ;CACF,CAAC;CAED,OAAOD,SAAMyC,YAAYC,SAAgC;EACvD,MAAMC,gBAAgB7B,MAAM;EAE5B,IAAI6B,kBAAkBX,KAAAA,GACpB;EAGF,MAAMY,MAAMtB,KAAKT,SAASS,KAAKT,OAAO8B,aAAa,IAAIA;EACvD,IAAID,SAASV,KAAAA,GAAW,OAAOY;EAC/B,QAAA,GAAA,sBAAA,kBAAwBF,MAAME,GAAG;CACnC,CAAC;AACH"}