sanity-plugin-utils 1.4.1 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,15 +1,7 @@
1
- > This is a **Sanity Studio v3** plugin.
2
-
3
- ## Installation
4
-
5
- ```sh
6
- npm install sanity-plugin-utils
7
- ```
8
-
9
- > This is exclusively a **Sanity Studio v3** plugin.
10
-
11
1
  # sanity-plugin-utils
12
2
 
3
+ Handy hooks and clever components for Sanity Studio v3.
4
+
13
5
  ## Installation
14
6
 
15
7
  ```
@@ -24,19 +16,17 @@ yarn add sanity-plugin-utils
24
16
 
25
17
  ## Usage
26
18
 
27
- Handy hooks and clever components for Sanity Studio v3
28
-
29
19
  ### `useListeningQuery()`
30
20
 
31
21
  Sanity's real-time APIs power excellent editorial experiences. Your plugins should respond to other users collaborating on documents in real time. This hook is a convenient way to perform a GROQ query that responds to changes, along with built-in `loading` and `error` states.
32
22
 
33
23
  The `data` variable will be constantly updated as changes are made to the data returned by your query. You can also pass in initial data so that it is set in the first render.
34
24
 
35
- ```jsx
25
+ ```tsx
36
26
  import {useListeningQuery} from 'sanity-plugin-utils'
37
27
 
38
28
  export default function DocumentList() {
39
- const {data, loading, error} = useListeningQuery(`*[_type == $type]`, {
29
+ const {data, loading, error} = useListeningQuery<SanityDocument[]>(`*[_type == $type]`, {
40
30
  params: {type: 'pet'},
41
31
  initialValue: [],
42
32
  })
@@ -65,7 +55,7 @@ export default function DocumentList() {
65
55
 
66
56
  Hook for getting extended details on all Users in the project. Such as name.
67
57
 
68
- ```jsx
58
+ ```tsx
69
59
  import {useProjectUsers} from 'sanity-plugin-utils'
70
60
 
71
61
  export default function DocumentList() {
@@ -103,11 +93,51 @@ export default function SidePetOpener(pet: SanityDocument) {
103
93
  }
104
94
  ```
105
95
 
96
+ ### useImageUrlBuilder()
97
+
98
+ Returns an [image URL builder](https://www.sanity.io/docs/image-url) configured with the current workspace's Project ID and Dataset.
99
+
100
+ Useful if you have many images in the one component.
101
+
102
+ ```tsx
103
+ import {useImageUrlBuilder} from 'sanity-plugin-utils'
104
+
105
+ export default function PetPics(pet: SanityDocument) {
106
+ const builder = useImageUrlBuilder({apiVersion: `2023-01-01`})
107
+
108
+ return (
109
+ <ul>
110
+ {pet.pics.map((pic) => (
111
+ <li key={pic._key}>
112
+ <img src={builder.source(pic).width(200).height(200).url()} alt={pic.altText} />
113
+ </li>
114
+ ))}
115
+ </ul>
116
+ )
117
+ }
118
+ ```
119
+
120
+ ### useImageUrlBuilderImage()
121
+
122
+ As above, but pre-configured with an image source.
123
+
124
+ Useful if you only have one image in your component.
125
+
126
+ ```tsx
127
+ import {useImageUrlBuilderImage} from 'sanity-plugin-utils'
128
+
129
+ export default function PetPic(pet: SanityDocument) {
130
+ const image = useImageUrlBuilderImage(pet.image, {apiVersion: `2023-01-01`})
131
+
132
+ return <img src={image.width(200).height(200).url()} alt={pet.name} />
133
+ }
134
+ ```
135
+
106
136
  ### Feedback
107
137
 
108
138
  Component for consistently displaying feedback in a card with a title, text and an icon.
109
139
 
110
- ```jsx
140
+ ```tsx
111
141
  import {Feedback, useListeningQuery} from 'sanity-plugin-utils'
112
142
 
113
143
  export default function DocumentList() {
@@ -135,7 +165,7 @@ export default function DocumentList() {
135
165
 
136
166
  These components are all `@sanity/ui` `Card`'s but with their HTML DOM elements and CSS updated to output and behave like tables.
137
167
 
138
- ```jsx
168
+ ```tsx
139
169
  import {Table, Row, Cell} from 'sanity-plugin-utils'
140
170
 
141
171
  export default function Report(documents) {
@@ -172,7 +202,7 @@ export default function Report(documents) {
172
202
 
173
203
  A Menu component for searching and interacting with users. Requires Users to be passed into the component.
174
204
 
175
- ```jsx
205
+ ```tsx
176
206
  import {UserSelectMenu} from 'sanity-plugin-utils'
177
207
 
178
208
  export default function Report() {
package/lib/index.d.ts CHANGED
@@ -1,12 +1,17 @@
1
1
  import {CardProps} from '@sanity/ui'
2
2
  import {CardTone} from '@sanity/ui'
3
+ import type {ImageUrlBuilder} from 'sanity'
4
+ import {ListenQueryOptions} from 'sanity'
5
+ import {ListenQueryParams} from 'sanity'
3
6
  import {PropsWithChildren} from 'react'
4
7
  import {default as React_2} from 'react'
8
+ import type {SanityImageSource} from '@sanity/asset-utils'
9
+ import type {SourceClientOptions} from 'sanity'
5
10
 
6
11
  export declare function Cell(props: TableCellProps): JSX.Element
7
12
 
8
13
  declare interface Config<V> {
9
- params: Params
14
+ params?: ListenQueryParams
10
15
  options?: ListenQueryOptions
11
16
  initialValue?: null | V
12
17
  }
@@ -42,13 +47,6 @@ declare type Labels = {
42
47
  notFound?: string
43
48
  }
44
49
 
45
- declare interface ListenQueryOptions {
46
- tag?: string
47
- apiVersion?: string
48
- }
49
-
50
- declare type Params = Record<string, string | number | boolean | string[]>
51
-
52
50
  declare interface Return<V> {
53
51
  loading: boolean
54
52
  error: boolean
@@ -71,6 +69,15 @@ declare type TableProps = PropsWithChildren<CardProps>
71
69
 
72
70
  declare type TableRowProps = PropsWithChildren<CardProps>
73
71
 
72
+ export declare function useImageUrlBuilder(
73
+ clientOptions?: SourceClientOptions
74
+ ): ImageUrlBuilder | null
75
+
76
+ export declare function useImageUrlBuilderImage(
77
+ source: SanityImageSource,
78
+ clientOptions?: SourceClientOptions
79
+ ): ImageUrlBuilder | null
80
+
74
81
  export declare function useListeningQuery<V>(
75
82
  query:
76
83
  | string