Skip to content

Commit e082db2

Browse files
authored
docs: update README to reflect event names and add React example (#84)
1 parent a7c2da8 commit e082db2

File tree

1 file changed

+99
-10
lines changed

1 file changed

+99
-10
lines changed

README.md

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ npm i @googleworkspace/drive-picker-element
4444
A CDN version is also available. See the [unpkg](https://unpkg.com/browse/@googleworkspace/drive-picker-element/dist/).
4545

4646
```html
47-
<script src="https://unpkg.com/@googleworkspace/drive-picker-element@0/dist/index.iife.min.js"></script>
47+
<script src="https://unpkg.com/@googleworkspace/drive-picker-element/dist/index.iife.min.js"></script>
4848
```
4949

5050
## Usage
@@ -95,7 +95,7 @@ If you already have an OAuth token, you can pass it to the `drive-picker` compon
9595
<drive-picker app-id="YOUR_APP_ID" oauth-token="OAUTH_TOKEN"></drive-picker>
9696
```
9797

98-
If you don't have an OAuth token, you can listen for the `"picker:authenticated"` event to get the token after the user has authenticated. This library wraps the [Google Identity Servies library](https://developers.google.com/identity/oauth2/web/guides/overview) to make it easier to authenticate users.
98+
If you don't have an OAuth token, you can listen for the `"picker-oauth-response"` event to get the token after the user has authenticated. This library wraps the [Google Identity Services library](https://developers.google.com/identity/oauth2/web/guides/overview) to make it easier to authenticate users.
9999

100100
### Listening to Events
101101

@@ -108,19 +108,20 @@ The `drive-picker` component emits several events that you can listen to. Here i
108108

109109
<script>
110110
const element = document.querySelector("drive-picker");
111-
element.addEventListener("picker:authenticated", console.log);
112-
element.addEventListener("picker:picked", console.log);
113-
element.addEventListener("picker:canceled", console.log);
111+
element.addEventListener("picker-oauth-response", console.log);
112+
element.addEventListener("picker-oauth-error", console.log);
113+
element.addEventListener("picker-picked", console.log);
114+
element.addEventListener("picker-canceled", console.log);
114115
</script>
115116
```
116117

117118
### Event Details
118119

119-
Most of these events return the [`Picker ResponseObject`](https://developers.google.com/drive/picker/reference/picker.responseobject) as the event detail. For example, the `"picker:picked"` `CustomEvent` contains details about the selected files:
120+
Most of these events return the [`Picker ResponseObject`](https://developers.google.com/drive/picker/reference/picker.responseobject) as the event detail. For example, the `"picker-picked"` `CustomEvent` contains details about the selected files:
120121

121122
```js
122123
{
123-
"type": "picker:picked",
124+
"type": "picker-picked",
124125
"detail": {
125126
"action": "picked",
126127
"docs": [
@@ -137,13 +138,16 @@ Most of these events return the [`Picker ResponseObject`](https://developers.goo
137138
}
138139
```
139140

140-
The `"picker:authenticated"` event returns the `token` as the event detail:
141+
The `"picker-oauth-response"` event returns the OAuth token response as the event detail:
141142

142143
```js
143144
{
144-
"type": "picker:authenticated",
145+
"type": "picker-oauth-response",
145146
"detail": {
146-
"token": "OMITTED"
147+
"access_token": "OMITTED",
148+
"expires_in": 3599,
149+
"scope": "https://www.googleapis.com/auth/drive.file",
150+
"token_type": "Bearer"
147151
}
148152
}
149153
```
@@ -194,6 +198,91 @@ declare global {
194198

195199
The above snippet can be added to a declaration file (e.g. `app.d.ts`) in your React project.
196200

201+
#### Using Events in React/Next.js
202+
203+
When working with React or Next.js, you need to use `useEffect` and `useRef` to properly attach event listeners to the web component. Here's a complete example:
204+
205+
```tsx
206+
import { useEffect, useRef, useState } from "react";
207+
import type {
208+
DrivePickerElement,
209+
PickerPickedEvent,
210+
} from "@googleworkspace/drive-picker-element";
211+
212+
export default function DrivePicker() {
213+
const pickerRef = useRef<DrivePickerElement>(null);
214+
const [selectedFiles, setSelectedFiles] = useState<any[]>([]);
215+
216+
useEffect(() => {
217+
// Dynamically import the web component
218+
import("@googleworkspace/drive-picker-element");
219+
}, []);
220+
221+
useEffect(() => {
222+
const pickerElement = pickerRef.current;
223+
if (!pickerElement) return;
224+
225+
// Use the non-deprecated event names (with hyphens, not colons)
226+
const handlePicked = (e: Event) => {
227+
const event = e as PickerPickedEvent;
228+
console.log("Files picked:", event.detail);
229+
setSelectedFiles(event.detail.docs || []);
230+
};
231+
232+
const handleCanceled = (e: Event) => {
233+
console.log("Picker canceled");
234+
};
235+
236+
const handleOAuthError = (e: Event) => {
237+
console.error("OAuth error:", e);
238+
};
239+
240+
// Add event listeners
241+
pickerElement.addEventListener("picker-picked", handlePicked);
242+
pickerElement.addEventListener("picker-canceled", handleCanceled);
243+
pickerElement.addEventListener("picker-oauth-error", handleOAuthError);
244+
245+
// Cleanup function to remove event listeners
246+
return () => {
247+
pickerElement.removeEventListener("picker-picked", handlePicked);
248+
pickerElement.removeEventListener("picker-canceled", handleCanceled);
249+
pickerElement.removeEventListener("picker-oauth-error", handleOAuthError);
250+
};
251+
}, []);
252+
253+
return (
254+
<div>
255+
<drive-picker
256+
ref={pickerRef}
257+
client-id="YOUR_CLIENT_ID"
258+
app-id="YOUR_APP_ID"
259+
>
260+
<drive-picker-docs-view></drive-picker-docs-view>
261+
</drive-picker>
262+
263+
{selectedFiles.length > 0 && (
264+
<div>
265+
<h3>Selected Files:</h3>
266+
<ul>
267+
{selectedFiles.map((file) => (
268+
<li key={file.id}>{file.name}</li>
269+
))}
270+
</ul>
271+
</div>
272+
)}
273+
</div>
274+
);
275+
}
276+
```
277+
278+
**Important notes for React/Next.js:**
279+
280+
1. **Dynamic import**: In Next.js, import the component dynamically inside `useEffect` to avoid server-side rendering issues, since web components need to run in the browser.
281+
282+
2. **Proper cleanup**: Always remove event listeners in the cleanup function to prevent memory leaks.
283+
284+
3. **Wait for the element**: Make sure the ref is populated before adding event listeners.
285+
197286
## Support
198287

199288
To report issues or feature requests for the underlying Drive Picker, please use the [Google Picker issue tracker](https://developers.google.com/drive/picker/support#developer_product_feedback). For all other issues, please use the [GitHub issue tracker](https://github.com/googleworkspace/drive-picker-element/issues).

0 commit comments

Comments
 (0)