Skip to content

Commit 4af57bb

Browse files
committed
fix: simplify events
1 parent 66bedea commit 4af57bb

File tree

4 files changed

+145
-47
lines changed

4 files changed

+145
-47
lines changed

README.md

+122-17
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66

77
## Description
88

9-
The [Google Drive Picker] web components provides a convenient way to integrate the Google Picker API into your web applications. The Google Picker API is a JavaScript API that allows users to select or upload Google Drive files. This component acts as a "File Open" dialog for accessing and interacting with files stored on Google Drive.
9+
The [Google Drive Picker] web component provides an easy way to integrate the Google Picker API into your web applications. The Google Picker API is a JavaScript API that allows users to select or upload Google Drive files. This component functions as a "File Open" dialog, enabling access to and interaction with files stored on Google Drive.
1010

1111
Try the [Demo](https://googleworkspace.github.io/drive-picker-element/?path=/docs/stories-drive-picker--docs).
1212

1313
## Index
1414

1515
- [Install](#install)
1616
- [Usage](#usage)
17+
- [Import the Component](#import-the-component)
18+
- [Add the Component to Your HTML](#add-the-component-to-your-html)
19+
- [Use the `oauth-token` attribute](#use-the-oauth-token-attribute)
20+
- [Listening to Events](#listening-to-events)
21+
- [Event Details](#event-details)
22+
- [Controlling Visibility](#controlling-visibility)
1723
- [Support](#support)
1824
- [Reference](#reference)
1925
- [`<drive-picker/>`](#drive-picker)
@@ -27,27 +33,128 @@ Install using NPM or similar.
2733
npm i @googleworkspace/drive-picker-element
2834
```
2935

30-
```sh
31-
yarn add @googleworkspace/drive-picker-element
32-
```
36+
A CDN version is also available. See the [unpkg](https://unpkg.com/browse/@googleworkspace/drive-picker-element/dist/).
3337

34-
```sh
35-
pnpm i @googleworkspace/drive-picker-element
38+
```html
39+
<script src="https://unpkg.com/@googleworkspace/drive-picker-element@0/dist/index.iife.min.js"></script>
3640
```
3741

3842
## Usage
3943

44+
To use the `drive-picker` component in your project, follow these steps:
45+
46+
### Import the Component
47+
48+
First, import the `drive-picker` component in your JavaScript file:
49+
4050
```js
4151
import "@googleworkspace/drive-picker-element";
4252
```
4353

54+
This isn't necessary if you're using the CDN version.
55+
56+
### Add the Component to Your HTML
57+
58+
Next, add the `drive-picker` component to your HTML file. Replace `YOUR_CLIENT_ID` and `YOUR_APP_ID` with your actual client ID and app ID.
59+
60+
> Note: You can find these values in the [Google Cloud Console](https://console.cloud.google.com/) under "APIs & Services" > "Credentials". You can also follow this guide to [create a new OAuth 2.0 client ID](https://developers.google.com/identity/oauth2/web/guides/get-google-api-clientid).
61+
4462
```html
4563
<drive-picker client-id="YOUR_CLIENT_ID" app-id="YOUR_APP_ID">
4664
<drive-picker-docs-view starred="true"></drive-picker-docs-view>
4765
</drive-picker>
4866
```
4967

50-
> Note: If you wish to register the component with a different name, `import { DrivePickerElement, DrivePickerDocsViewElement } from @googleworkspace/drive-picker-element/drive-picker` and call `customElements.define()` manually.
68+
> Note: If you wish to register the component with a different name, you can import the components individually and call `customElements.define()` manually:
69+
70+
```js
71+
import {
72+
DrivePickerElement,
73+
DrivePickerDocsViewElement,
74+
} from "@googleworkspace/drive-picker-element/drive-picker";
75+
customElements.define("custom-drive-picker", DrivePickerElement);
76+
customElements.define(
77+
"custom-drive-picker-docs-view",
78+
DrivePickerDocsViewElement,
79+
);
80+
```
81+
82+
### Use the `oauth-token` attribute
83+
84+
If you already have an OAuth token, you can pass it to the `drive-picker` component using the `oauth-token` attribute. This will authenticate the user without requiring them to sign in again.
85+
86+
```html
87+
<drive-picker app-id="YOUR_APP_ID" oauth="OAUTH_TOKEN"></drive-picker>
88+
```
89+
90+
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.
91+
92+
### Listening to Events
93+
94+
The `drive-picker` component emits several events that you can listen to. Here is an example of how to listen to these events:
95+
96+
```html
97+
<drive-picker client-id="YOUR_CLIENT_ID" app-id="YOUR_APP_ID">
98+
<drive-picker-docs-view starred="true"></drive-picker-docs-view>
99+
</drive-picker>
100+
101+
<script>
102+
const element = document.querySelector("drive-picker");
103+
element.addEventListener("picker:authenticated", console.log);
104+
element.addEventListener("picker:loaded", console.log);
105+
element.addEventListener("picker:picked", console.log);
106+
element.addEventListener("picker:canceled", console.log);
107+
</script>
108+
```
109+
110+
### Event Details
111+
112+
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:
113+
114+
```js
115+
{
116+
"type": "picker:picked",
117+
"detail": {
118+
"action": "picked",
119+
"docs": [
120+
{
121+
"id": "OMITTED",
122+
"mimeType": "application/pdf",
123+
"name": "OMITTED",
124+
"url": "https://drive.google.com/file/d/OMITTED/view?usp=drive_web",
125+
"sizeBytes": 12345
126+
// ... other properties omitted
127+
}
128+
]
129+
}
130+
}
131+
```
132+
133+
The `"picker:authenticated"` event returns the `token` as the event detail:
134+
135+
```js
136+
{
137+
"type": "picker:authenticated",
138+
"detail": {
139+
"token": "OMITTED"
140+
}
141+
}
142+
```
143+
144+
### Controlling Visibility
145+
146+
To make the picker visible, set the `visible` property of the `drive-picker` element to `true`:
147+
148+
```html
149+
<drive-picker client-id="YOUR_CLIENT_ID" app-id="YOUR_APP_ID"></drive-picker>
150+
151+
<script>
152+
const element = document.querySelector("drive-picker");
153+
element.visible = true;
154+
</script>
155+
```
156+
157+
After the picker dialog has been closed, the `visible` property will be reset to `false`.
51158

52159
## Support
53160

@@ -62,9 +169,8 @@ To report issues or feature requests for the underlying Drive Picker, please use
62169
The `drive-picker` web component provides a convenient way to declaratively
63170
build
64171
[`google.picker.Picker`](https://developers.google.com/drive/picker/reference/picker)
65-
by using the component attributes and
66-
[`google.picker.PickerBuilder`](https://developers.google.com/drive/picker/reference/picker.pickerbuilder)
67-
and load OAuth tokens.
172+
by using the component attributes mapped to the corresponding methods of
173+
[`google.picker.PickerBuilder`](https://developers.google.com/drive/picker/reference/picker.pickerbuilder).
68174

69175
#### Attributes
70176

@@ -87,13 +193,12 @@ and load OAuth tokens.
87193

88194
#### Events
89195

90-
| Name | Type | Description |
91-
| ---------------------- | ------------- | ---------------------------------------------------------------------------------- |
92-
| `picker:authenticated` | `CustomEvent` | Triggered when the user authenticates with the provided OAuth client ID and scope. |
93-
| `picker:canceled` | \`\` | Triggered when the user cancels the picker dialog. |
94-
| `picker:picked` | \`\` | Triggered when the user picks one or more items. |
95-
| `picker:loaded` | \`\` | Triggered when the picker is loaded. |
96-
| `picker:error` | \`\` | Triggered when an error occurs. |
196+
| Name | Type | Description |
197+
| ---------------------- | ------------------------------ | ---------------------------------------------------------------------------------- |
198+
| `picker:authenticated` | `{ token: string }` | Triggered when the user authenticates with the provided OAuth client ID and scope. |
199+
| `picker:canceled` | `google.picker.ResponseObject` | Triggered when the user cancels the picker dialog. |
200+
| `picker:picked` | `google.picker.ResponseObject` | Triggered when the user picks one or more items. |
201+
| `picker:error` | `google.picker.ResponseObject` | Triggered when an error occurs. |
97202

98203
#### Slots
99204

custom-elements.json

+12-7
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
"declarations": [
172172
{
173173
"kind": "class",
174-
"description": "The `drive-picker` web component provides a convenient way to declaratively\nbuild\n[`google.picker.Picker`](https://developers.google.com/drive/picker/reference/picker)\nby using the component attributes and\n[`google.picker.PickerBuilder`](https://developers.google.com/drive/picker/reference/picker.pickerbuilder)\nand load OAuth tokens.",
174+
"description": "The `drive-picker` web component provides a convenient way to declaratively\nbuild\n[`google.picker.Picker`](https://developers.google.com/drive/picker/reference/picker)\nby using the component attributes mapped to the corresponding methods of\n[`google.picker.PickerBuilder`](https://developers.google.com/drive/picker/reference/picker.pickerbuilder).",
175175
"name": "DrivePickerElement",
176176
"slots": [
177177
{
@@ -242,7 +242,7 @@
242242
"privacy": "private",
243243
"parameters": [
244244
{
245-
"name": "data",
245+
"name": "detail",
246246
"type": {
247247
"text": "google.picker.ResponseObject"
248248
}
@@ -254,7 +254,7 @@
254254
{
255255
"name": "picker:authenticated",
256256
"type": {
257-
"text": "CustomEvent"
257+
"text": "{ token: string }"
258258
},
259259
"description": "Triggered when the user authenticates with the provided OAuth client ID and scope."
260260
},
@@ -265,18 +265,23 @@
265265
}
266266
},
267267
{
268+
"type": {
269+
"text": "google.picker.ResponseObject"
270+
},
268271
"description": "Triggered when the user cancels the picker dialog.",
269272
"name": "picker:canceled"
270273
},
271274
{
275+
"type": {
276+
"text": "google.picker.ResponseObject"
277+
},
272278
"description": "Triggered when the user picks one or more items.",
273279
"name": "picker:picked"
274280
},
275281
{
276-
"description": "Triggered when the picker is loaded.",
277-
"name": "picker:loaded"
278-
},
279-
{
282+
"type": {
283+
"text": "google.picker.ResponseObject"
284+
},
280285
"description": "Triggered when an error occurs.",
281286
"name": "picker:error"
282287
}

src/drive-picker/drive-picker-element.ts

+11-21
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,11 @@ interface DrivePickerDocsViewElement extends HTMLElement {
2828
view: google.picker.DocsView;
2929
}
3030

31-
type DrivePickerEventDetail = google.picker.ResponseObject & {
32-
picker: google.picker.Picker;
33-
};
34-
3531
declare global {
3632
interface GlobalEventHandlersEventMap {
3733
"picker:authenticated": CustomEvent<{ token: string }>;
38-
"picker:canceled": CustomEvent<DrivePickerEventDetail>;
39-
"picker:picked": CustomEvent<DrivePickerEventDetail>;
40-
"picker:loaded": CustomEvent<DrivePickerEventDetail>;
34+
"picker:canceled": CustomEvent<google.picker.ResponseObject>;
35+
"picker:picked": CustomEvent<google.picker.ResponseObject>;
4136
"picker:error": CustomEvent<unknown>;
4237
}
4338
}
@@ -46,18 +41,16 @@ declare global {
4641
* The `drive-picker` web component provides a convenient way to declaratively
4742
* build
4843
* [`google.picker.Picker`](https://developers.google.com/drive/picker/reference/picker)
49-
* by using the component attributes and
50-
* [`google.picker.PickerBuilder`](https://developers.google.com/drive/picker/reference/picker.pickerbuilder)
51-
* and load OAuth tokens.
44+
* by using the component attributes mapped to the corresponding methods of
45+
* [`google.picker.PickerBuilder`](https://developers.google.com/drive/picker/reference/picker.pickerbuilder).
5246
*
5347
* @element drive-picker
5448
*
55-
* @fires picker:authenticated - Triggered when the user authenticates with the
49+
* @fires {{ token: string }} picker:authenticated - Triggered when the user authenticates with the
5650
* provided OAuth client ID and scope.
57-
* @fires picker:canceled - Triggered when the user cancels the picker dialog.
58-
* @fires picker:picked - Triggered when the user picks one or more items.
59-
* @fires picker:loaded - Triggered when the picker is loaded.
60-
* @fires picker:error - Triggered when an error occurs.
51+
* @fires {google.picker.ResponseObject} picker:canceled - Triggered when the user cancels the picker dialog.
52+
* @fires {google.picker.ResponseObject} picker:picked - Triggered when the user picks one or more items.
53+
* @fires {google.picker.ResponseObject} picker:error - Triggered when an error occurs.
6154
*
6255
* @slot - The default slot contains View elements to display in the picker.
6356
* Each View element should implement a property `view` of type
@@ -255,10 +248,10 @@ export class DrivePickerElement extends HTMLElement {
255248
});
256249
}
257250

258-
private callbackToDispatchEvent(data: google.picker.ResponseObject) {
251+
private callbackToDispatchEvent(detail: google.picker.ResponseObject) {
259252
let eventType: keyof GlobalEventHandlersEventMap;
260253

261-
switch (data.action) {
254+
switch (detail.action) {
262255
case google.picker.Action.CANCEL:
263256
eventType = "picker:canceled";
264257
break;
@@ -268,16 +261,13 @@ export class DrivePickerElement extends HTMLElement {
268261
case google.picker.Action.ERROR:
269262
eventType = "picker:error";
270263
break;
271-
case "loaded":
272-
eventType = "picker:loaded";
273-
break;
274264
default:
275265
return;
276266
}
277267

278268
this.dispatchEvent(
279269
new CustomEvent(eventType, {
280-
detail: { ...data, picker: this.picker },
270+
detail,
281271
}),
282272
);
283273
}

src/stories/drive-picker.stories.ts

-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import { APP_ID, CLIENT_ID, META_PARAMETERS } from "./utils/common";
2424
import "./utils/lazy-preview-element";
2525
import "..";
2626

27-
const argTypes = elementArgTypes["drive-picker"];
28-
2927
const elementEventNames = getElementEvents("drive-picker").map(
3028
(event) => event.name,
3129
);

0 commit comments

Comments
 (0)