Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 99 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ npm i @googleworkspace/drive-picker-element
A CDN version is also available. See the [unpkg](https://unpkg.com/browse/@googleworkspace/drive-picker-element/dist/).

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

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

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.
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.

### Listening to Events

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

<script>
const element = document.querySelector("drive-picker");
element.addEventListener("picker:authenticated", console.log);
element.addEventListener("picker:picked", console.log);
element.addEventListener("picker:canceled", console.log);
element.addEventListener("picker-oauth-response", console.log);
element.addEventListener("picker-oauth-error", console.log);
element.addEventListener("picker-picked", console.log);
element.addEventListener("picker-canceled", console.log);
</script>
```

### Event Details

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:
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:

```js
{
"type": "picker:picked",
"type": "picker-picked",
"detail": {
"action": "picked",
"docs": [
Expand All @@ -137,13 +138,16 @@ Most of these events return the [`Picker ResponseObject`](https://developers.goo
}
```

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

```js
{
"type": "picker:authenticated",
"type": "picker-oauth-response",
"detail": {
"token": "OMITTED"
"access_token": "OMITTED",
"expires_in": 3599,
"scope": "https://www.googleapis.com/auth/drive.file",
"token_type": "Bearer"
}
}
```
Expand Down Expand Up @@ -194,6 +198,91 @@ declare global {

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

#### Using Events in React/Next.js

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:

```tsx
import { useEffect, useRef, useState } from "react";
import type {
DrivePickerElement,
PickerPickedEvent,
} from "@googleworkspace/drive-picker-element";

export default function DrivePicker() {
const pickerRef = useRef<DrivePickerElement>(null);
const [selectedFiles, setSelectedFiles] = useState<any[]>([]);

useEffect(() => {
// Dynamically import the web component
import("@googleworkspace/drive-picker-element");
}, []);

useEffect(() => {
const pickerElement = pickerRef.current;
if (!pickerElement) return;

// Use the non-deprecated event names (with hyphens, not colons)
const handlePicked = (e: Event) => {
const event = e as PickerPickedEvent;
console.log("Files picked:", event.detail);
setSelectedFiles(event.detail.docs || []);
};

const handleCanceled = (e: Event) => {
console.log("Picker canceled");
};

const handleOAuthError = (e: Event) => {
console.error("OAuth error:", e);
};

// Add event listeners
pickerElement.addEventListener("picker-picked", handlePicked);
pickerElement.addEventListener("picker-canceled", handleCanceled);
pickerElement.addEventListener("picker-oauth-error", handleOAuthError);

// Cleanup function to remove event listeners
return () => {
pickerElement.removeEventListener("picker-picked", handlePicked);
pickerElement.removeEventListener("picker-canceled", handleCanceled);
pickerElement.removeEventListener("picker-oauth-error", handleOAuthError);
};
}, []);

return (
<div>
<drive-picker
ref={pickerRef}
client-id="YOUR_CLIENT_ID"
app-id="YOUR_APP_ID"
>
<drive-picker-docs-view></drive-picker-docs-view>
</drive-picker>

{selectedFiles.length > 0 && (
<div>
<h3>Selected Files:</h3>
<ul>
{selectedFiles.map((file) => (
<li key={file.id}>{file.name}</li>
))}
</ul>
</div>
)}
</div>
);
}
```

**Important notes for React/Next.js:**

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.

2. **Proper cleanup**: Always remove event listeners in the cleanup function to prevent memory leaks.

3. **Wait for the element**: Make sure the ref is populated before adding event listeners.

## Support

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).
Expand Down