Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

inlineCss.ts

41 lines1.2 KB

1import type { Awaitable } from '@tanstack/router-core'
2​
3export type HandlerInlineCssOption =
4 | boolean
5 | ((ctx: { request: Request }) => Awaitable<boolean>)
6​
7/**
8 * The inline-CSS choice that needs no handler callback: a request override
9 * wins, then a static handler option, then the default. Undefined when a
10 * handler callback has to decide.
11 */
12export function getStaticInlineCss(
13 requestInlineCss: boolean | undefined,
14 handlerInlineCss: HandlerInlineCssOption | undefined,
15): boolean | undefined {
16 if (requestInlineCss !== undefined) {
17 return requestInlineCss
18 }
19 if (typeof handlerInlineCss === 'function') {
20 return undefined
21 }
22 return handlerInlineCss ?? true
24​
25export async function resolveInlineCssForRequest(opts: {
26 request: Request
27 handlerInlineCss: HandlerInlineCssOption | undefined
28 requestInlineCss: boolean | undefined
29}) {
30 const inlineCss = getStaticInlineCss(
31 opts.requestInlineCss,
32 opts.handlerInlineCss,
33 )
34 if (inlineCss !== undefined) {
35 return inlineCss
36 }
37 // Only a handler callback leaves the choice open.
38 return await (
39 opts.handlerInlineCss as Exclude<HandlerInlineCssOption, boolean>
40 )({ request: opts.request })