@okam/directus-next-component
Advanced tools
Comparing version 1.2.0 to 1.3.0
"use strict"; | ||
const jsxRuntime = require("react/jsx-runtime"); | ||
const stackUi = require("@okam/stack-ui"); | ||
const Link = require("next/link.js"); | ||
const directusLink = require("../../hooks/directus-link.js"); | ||
@@ -9,7 +8,8 @@ const DirectusLink = (props) => { | ||
const linkProps = directusLink(props); | ||
if (!type || !linkProps.href) return null; | ||
if (!type) return null; | ||
const LinkComponent = componentsConfig == null ? void 0 : componentsConfig[type]; | ||
if (!LinkComponent) return /* @__PURE__ */ jsxRuntime.jsx(stackUi.Anchor, { as: Link, ...linkProps }); | ||
return /* @__PURE__ */ jsxRuntime.jsx(LinkComponent, { ...props }); | ||
if (LinkComponent) return /* @__PURE__ */ jsxRuntime.jsx(LinkComponent, { ...props }); | ||
if (!linkProps.href) return null; | ||
return /* @__PURE__ */ jsxRuntime.jsx(stackUi.Anchor, { ...linkProps }); | ||
}; | ||
module.exports = DirectusLink; |
import type { TDefaultComponent, TAnchorProps } from '@okam/stack-ui'; | ||
import type { ComponentType } from 'react'; | ||
import type { AriaAttributes, ComponentType } from 'react'; | ||
import type { TLinks } from '../../types/links'; | ||
export interface TDirectusLinkProps extends Omit<TDefaultComponent, 'children'>, Omit<Partial<TLinks>, 'tokens'> { | ||
export type TDirectusLink = Omit<TDefaultComponent, 'children'> & Omit<Partial<TLinks>, 'tokens'> & AriaAttributes; | ||
export interface TUseDirectusLink extends TDirectusLink { | ||
propsConfig?: TDirectusLinkPropsConfig; | ||
} | ||
export interface TDirectusLinkProps extends TDirectusLink { | ||
componentsConfig?: TDirectusLinkComponentsConfig; | ||
@@ -9,2 +13,2 @@ propsConfig?: TDirectusLinkPropsConfig; | ||
export type TDirectusLinkComponentsConfig = Record<string, ComponentType<TDirectusLinkProps>>; | ||
export type TDirectusLinkPropsConfig<ReturnProps extends TAnchorProps = TAnchorProps> = Record<string, (props: TDirectusLinkProps) => ReturnProps>; | ||
export type TDirectusLinkPropsConfig<ReturnProps extends TAnchorProps = TAnchorProps> = Record<string, (props: TUseDirectusLink) => ReturnProps>; |
import type { Nullable } from '@okam/stack-ui'; | ||
import type { TFiles } from '../types/files'; | ||
export default function useDirectusFile(file: Nullable<TFiles>, baseUrl?: URL): URL | null; | ||
export default function useDirectusFile(file: Nullable<TFiles>, baseUrl?: URL, searchParams?: Record<string, Nullable<string>>): { | ||
tags?: any; | ||
location?: string | null | undefined; | ||
storage?: string | null | undefined; | ||
focal_point_divider?: string | null | undefined; | ||
focal_point_x?: number | null | undefined; | ||
focal_point_y?: number | null | undefined; | ||
filename_disk?: string | null | undefined; | ||
filename_download?: string | null | undefined; | ||
type?: string | null | undefined; | ||
duration?: number | null | undefined; | ||
embed?: string | null | undefined; | ||
caption?: string | null | undefined; | ||
thumbhash?: string | null | undefined; | ||
filenameDownload?: string | null | undefined; | ||
src: string; | ||
alt: string; | ||
width: number | undefined; | ||
height: number | undefined; | ||
title: string | undefined; | ||
id: string; | ||
} | null; |
"use strict"; | ||
const IMG_DOMAIN = process.env.NEXT_PUBLIC_IMG_DOMAIN; | ||
const IMG_PORT = process.env.NEXT_PUBLIC_IMG_PORT; | ||
const IMG_PROTOCOL = process.env.NEXT_PUBLIC_IMG_PROTOCOL; | ||
function getDirectusUrl(file, baseUrl) { | ||
const IMG_PROTOCOL = process.env.NEXT_PUBLIC_IMG_PROTOCOL ?? "https"; | ||
function setSearchParams(url, searchParams) { | ||
Object.entries(searchParams).forEach(([key, value]) => { | ||
if (!value) return; | ||
url.searchParams.set(key, value); | ||
}); | ||
} | ||
function getDirectusUrl(file, baseUrl, searchParams) { | ||
const { id, filename_download, filenameDownload } = file ?? {}; | ||
@@ -10,6 +16,5 @@ const { protocol = IMG_PROTOCOL, port = IMG_PORT, hostname = IMG_DOMAIN } = baseUrl ?? {}; | ||
try { | ||
const url = new URL( | ||
`/assets/${id}/${filename_download ?? filenameDownload ?? ""}`, | ||
`${protocol ?? "https"}://${hostname}${port ? `:${port}` : ""}` | ||
); | ||
const url = new URL(`/assets/${id}/${filename_download ?? filenameDownload ?? ""}`, `${protocol}://${hostname}`); | ||
if (port) url.port = port; | ||
if (searchParams) setSearchParams(url, searchParams); | ||
return url; | ||
@@ -21,7 +26,17 @@ } catch (error) { | ||
} | ||
function useDirectusFile(file, baseUrl) { | ||
if (!file) return null; | ||
const url = getDirectusUrl(file, baseUrl); | ||
return url; | ||
function useDirectusFile(file, baseUrl, searchParams) { | ||
const { description, width, height, title, id, ...rest } = file ?? {}; | ||
if (!file || !id) return null; | ||
const url = getDirectusUrl(file, baseUrl, searchParams); | ||
if (!url) return null; | ||
return { | ||
src: url.href, | ||
alt: description ?? "", | ||
width: width ?? void 0, | ||
height: height ?? void 0, | ||
title: title ?? void 0, | ||
id, | ||
...rest | ||
}; | ||
} | ||
module.exports = useDirectusFile; |
import type { TAnchorProps } from '@okam/stack-ui'; | ||
import type { TDirectusLinkProps } from '../components/DirectusLink/interface'; | ||
export default function useDirectusLink(props: TDirectusLinkProps): TAnchorProps; | ||
import type { TUseDirectusLink } from '../components/DirectusLink/interface'; | ||
export default function useDirectusLink(props: TUseDirectusLink): TAnchorProps; |
"use strict"; | ||
const Link = require("next/link.js"); | ||
const directusFile = require("./directus-file.js"); | ||
function useFile(props) { | ||
const { file } = props; | ||
const { filename_download: filenameDownload } = file ?? {}; | ||
const href = directusFile(file); | ||
return { | ||
href: (href == null ? void 0 : href.toString()) ?? void 0, | ||
download: filenameDownload ?? true | ||
}; | ||
} | ||
function useCollection(props) { | ||
var _a, _b; | ||
const { collection, target } = props; | ||
return { | ||
href: ((_b = (_a = collection == null ? void 0 : collection.translations) == null ? void 0 : _a[0]) == null ? void 0 : _b.path) ?? void 0, | ||
target: target ?? void 0 | ||
}; | ||
} | ||
function useExternalLink(props) { | ||
const { external_link: externalLink, target } = props; | ||
return { | ||
href: externalLink ?? void 0, | ||
target: target ?? void 0 | ||
}; | ||
} | ||
function useAnchor(props) { | ||
const { anchor } = props; | ||
return { href: anchor ?? void 0 }; | ||
} | ||
const defaultPropsConfig = { | ||
collection: ({ collection, target }) => { | ||
var _a, _b; | ||
return { | ||
href: ((_b = (_a = collection == null ? void 0 : collection.translations) == null ? void 0 : _a[0]) == null ? void 0 : _b.path) ?? void 0, | ||
target: target ?? void 0 | ||
}; | ||
}, | ||
collection: useCollection, | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
"external-link": ({ external_link: externalLink, target }) => ({ | ||
href: externalLink ?? void 0, | ||
target: target ?? void 0 | ||
}), | ||
file: ({ file }) => { | ||
const { filename_download: filenameDownload } = file ?? {}; | ||
const href = directusFile(file); | ||
return { | ||
href: (href == null ? void 0 : href.toString()) ?? void 0, | ||
download: filenameDownload ?? true | ||
}; | ||
}, | ||
anchor: ({ anchor }) => ({ href: anchor ?? void 0 }) | ||
"external-link": useExternalLink, | ||
file: useFile, | ||
anchor: useAnchor | ||
}; | ||
function useDirectusLink(props) { | ||
const { type, label, prefetch, replace, scroll, variant, tokens, themeName, customTheme, propsConfig } = props; | ||
var _a; | ||
const { | ||
type, | ||
label, | ||
prefetch, | ||
replace, | ||
scroll, | ||
tokens, | ||
themeName, | ||
customTheme, | ||
propsConfig, | ||
as = Link, | ||
target, | ||
anchor, | ||
collection, | ||
external_link: externalLink, | ||
file, | ||
id, | ||
...rest | ||
} = props; | ||
if (!type) return {}; | ||
const finalConfig = { ...defaultPropsConfig, ...propsConfig ?? {} }; | ||
const { href, ...rest } = finalConfig[type](props); | ||
const linkProps = ((_a = finalConfig[type]) == null ? void 0 : _a.call(finalConfig, props)) ?? {}; | ||
const { href, ...restOfLinkProps } = linkProps; | ||
if (!href) return {}; | ||
return { | ||
themeName, | ||
tokens: { ...tokens, ...variant ? { type: variant } : {} }, | ||
customTheme, | ||
...rest, | ||
as, | ||
...themeName ? { themeName } : {}, | ||
...customTheme ? { customTheme } : {}, | ||
...tokens ? { tokens } : {}, | ||
nextLinkProps: { | ||
@@ -44,5 +79,5 @@ href, | ||
children: label, | ||
...rest | ||
...restOfLinkProps | ||
}; | ||
} | ||
module.exports = useDirectusLink; |
@@ -7,4 +7,5 @@ import type { Nullable } from '@okam/stack-ui'; | ||
* @param navigationItems A tree of navigation items, with parents and children | ||
* @param onNavigationItem Called when a navigation item is about to be added to the tree | ||
* @returns A tree of navigation items with ready-to-use DirectusLink components | ||
*/ | ||
export default function useNavigationItems<Depth extends number, Link, NavigationItems extends TNavigationItems<NavigationItems, Link, Depth> = TNavigationItems<unknown, Link, Depth>>(items: Nullable<Nullable<TNavigationItems<NavigationItems, Link, Depth>>[]>, onNavigationItem: (item: Nullable<TNavigationItems<NavigationItems, Link, Depth>>) => Nullable<TDirectusLinkProps>): Nullable<Nullable<TNavigationItemsTree>[]>; |
"use strict"; | ||
const jsxRuntime = require("react/jsx-runtime"); | ||
const index = require("../components/DirectusLink/index.js"); | ||
function createLinkProps(item, onNavigationItem) { | ||
const { tokens, variant } = item ?? {}; | ||
const link = onNavigationItem(item); | ||
const linkTokens = { | ||
...tokens, | ||
...link == null ? void 0 : link.tokens, | ||
type: variant | ||
}; | ||
const linkProps = { | ||
...link, | ||
tokens: linkTokens | ||
}; | ||
return linkProps; | ||
} | ||
function createParentTree(item, onNavigationItem, depth = -1) { | ||
const { parent } = item ?? {}; | ||
const link = onNavigationItem(item); | ||
const { id } = link ?? {}; | ||
const linkProps = createLinkProps(item, onNavigationItem); | ||
const { id } = linkProps ?? {}; | ||
if (!id) return null; | ||
return { | ||
link: /* @__PURE__ */ jsxRuntime.jsx(index, { ...link }), | ||
linkProps: link, | ||
link: /* @__PURE__ */ jsxRuntime.jsx(index, { ...linkProps }), | ||
linkProps, | ||
parent: createParentTree(parent, onNavigationItem, depth - 1), | ||
@@ -18,8 +32,8 @@ depth | ||
const { children } = item ?? {}; | ||
const link = onNavigationItem(item); | ||
const { id } = link ?? {}; | ||
const linkProps = createLinkProps(item, onNavigationItem); | ||
const { id } = linkProps ?? {}; | ||
if (!id) return null; | ||
return { | ||
link: /* @__PURE__ */ jsxRuntime.jsx(index, { ...link }), | ||
linkProps: link, | ||
link: /* @__PURE__ */ jsxRuntime.jsx(index, { ...linkProps }), | ||
linkProps, | ||
children: children == null ? void 0 : children.map((child) => createChildrenTree(child, onNavigationItem, depth + 1)), | ||
@@ -32,3 +46,3 @@ depth | ||
const { children, parent } = item ?? {}; | ||
const link = onNavigationItem(item); | ||
const linkProps = createLinkProps(item, onNavigationItem); | ||
const parentTree = createParentTree(parent, onNavigationItem); | ||
@@ -40,4 +54,4 @@ const childrenTree = children == null ? void 0 : children.map((child) => createChildrenTree(child, onNavigationItem)); | ||
children: childrenTree, | ||
link: /* @__PURE__ */ jsxRuntime.jsx(index, { ...link }), | ||
linkProps: link | ||
link: /* @__PURE__ */ jsxRuntime.jsx(index, { ...linkProps }), | ||
linkProps | ||
}; | ||
@@ -44,0 +58,0 @@ }); |
export { default as DirectusLink } from './components/DirectusLink'; | ||
export { default as DirectusImg } from './components/DirectusImg'; | ||
export { default as DirectusVideo } from './components/DirectusVideo'; | ||
export { default as DirectusFile } from './components/DirectusFile'; | ||
export { default as useDirectusFile } from './hooks/directus-file'; | ||
@@ -9,2 +12,2 @@ export { default as useDirectusLink } from './hooks/directus-link'; | ||
export type { TLinks } from './types/links'; | ||
export type { TDirectusLinkProps } from './components/DirectusLink/interface'; | ||
export type { TDirectusLinkProps, TUseDirectusLink, TDirectusLink, TDirectusLinkPropsConfig, TDirectusLinkComponentsConfig, } from './components/DirectusLink/interface'; |
"use strict"; | ||
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
const index = require("./components/DirectusLink/index.js"); | ||
const index$1 = require("./components/DirectusImg/index.js"); | ||
const index$2 = require("./components/DirectusVideo/index.js"); | ||
const index$3 = require("./components/DirectusFile/index.js"); | ||
const directusFile = require("./hooks/directus-file.js"); | ||
@@ -8,4 +11,7 @@ const directusLink = require("./hooks/directus-link.js"); | ||
exports.DirectusLink = index; | ||
exports.DirectusImg = index$1; | ||
exports.DirectusVideo = index$2; | ||
exports.DirectusFile = index$3; | ||
exports.useDirectusFile = directusFile; | ||
exports.useDirectusLink = directusLink; | ||
exports.useNavigationItems = navigationItems; |
{ | ||
"name": "@okam/directus-next-component", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"main": "./index.js", | ||
@@ -5,0 +5,0 @@ "types": "./index.d.ts", |
@@ -1,7 +0,9 @@ | ||
export * from './lib/hello-server'; | ||
export { default as withDirectus } from './lib/withDirectus'; | ||
export * from './lib/directus-next-rewrite'; | ||
export { default as DirectusLink } from './components/DirectusLink'; | ||
export { default as DirectusImg } from './components/DirectusImg'; | ||
export { default as DirectusVideo } from './components/DirectusVideo'; | ||
export { default as DirectusFile } from './components/DirectusFile'; | ||
export { default as useDirectusFile } from './hooks/directus-file'; | ||
export { default as useDirectusLink } from './hooks/directus-link'; | ||
export { default as useNavigationItems } from './hooks/navigation-items'; |
"use strict"; | ||
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
const helloServer = require("./lib/hello-server.js"); | ||
const withDirectus = require("./lib/withDirectus.js"); | ||
const directusNextRewrite = require("./lib/directus-next-rewrite.js"); | ||
const index = require("./components/DirectusLink/index.js"); | ||
const index$1 = require("./components/DirectusImg/index.js"); | ||
const index$2 = require("./components/DirectusVideo/index.js"); | ||
const index$3 = require("./components/DirectusFile/index.js"); | ||
const directusFile = require("./hooks/directus-file.js"); | ||
const directusLink = require("./hooks/directus-link.js"); | ||
const navigationItems = require("./hooks/navigation-items.js"); | ||
exports.HelloServer = helloServer.HelloServer; | ||
exports.withDirectus = withDirectus; | ||
@@ -16,4 +17,7 @@ exports.config = directusNextRewrite.config; | ||
exports.DirectusLink = index; | ||
exports.DirectusImg = index$1; | ||
exports.DirectusVideo = index$2; | ||
exports.DirectusFile = index$3; | ||
exports.useDirectusFile = directusFile; | ||
exports.useDirectusLink = directusLink; | ||
exports.useNavigationItems = navigationItems; |
export type TFiles = { | ||
id: string; | ||
id?: string | null; | ||
title?: string | null; | ||
description?: string | null; | ||
tags?: any | null; | ||
location?: string | null; | ||
storage?: string | null; | ||
focal_point_divider?: string | null; | ||
focal_point_x?: number | null; | ||
focal_point_y?: number | null; | ||
filename_disk?: string | null; | ||
filename_download?: string | null; | ||
/** | ||
* @deprecated | ||
*/ | ||
filenameDownload?: string | null; | ||
title?: string | null; | ||
type?: string | null; | ||
@@ -15,5 +18,8 @@ width?: number | null; | ||
embed?: string | null; | ||
description?: string | null; | ||
focal_point_x?: number | null; | ||
focal_point_y?: number | null; | ||
caption?: string | null; | ||
thumbhash?: string | null; | ||
/** | ||
* @deprecated Use `filename_download` instead | ||
*/ | ||
filenameDownload?: string | null; | ||
}; |
@@ -15,7 +15,5 @@ import type { TFiles } from './files'; | ||
target?: string | null; | ||
tokens?: any | null; | ||
type?: string | null; | ||
variant?: string | null; | ||
collection?: TPageSettings | null; | ||
file?: TFiles | null; | ||
}; |
@@ -20,9 +20,13 @@ /// <reference types="react" /> | ||
type DepthLimit = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
export type TNavigationItemsParents<NavigationItems, Link, Depth extends number> = Depth extends never ? Link : { | ||
export type TNavigationItemsBase<Link> = { | ||
tokens?: any | null; | ||
variant?: string | null; | ||
} & Link; | ||
export type TNavigationItemsParents<NavigationItems, Link, Depth extends number> = Depth extends never ? TNavigationItemsBase<Link> : { | ||
parent?: Nullable<TNavigationItemsParents<NavigationItems, Link, Depth>>; | ||
} & Link; | ||
export type TNavigationItemsChildren<NavigationItems, Link, Depth extends number> = Depth extends never ? Link : { | ||
} & TNavigationItemsBase<Link>; | ||
export type TNavigationItemsChildren<NavigationItems, Link, Depth extends number> = Depth extends never ? TNavigationItemsBase<Link> : { | ||
children?: Nullable<Nullable<TNavigationItemsChildren<NavigationItems, Link, DepthLimit[Depth]>>[]>; | ||
} & Link; | ||
} & TNavigationItemsBase<Link>; | ||
export type TNavigationItems<NavigationItems, Link, Depth extends number> = TNavigationItemsChildren<NavigationItems, Link, Depth> & TNavigationItemsParents<NavigationItems, Link, Depth>; | ||
export {}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
36383
45
998
2