## class: `WyFileNew`, `wy-file-new`

Weavy file component to create files, upload files or attach cloud files to a files app.

Supports upload of multiple large files at once, upload using drag'n'drop and pasting files to upload. The file upload progress can be monitored and aborted.

Prompts for a filename when creating files, optionally suggesting an extension. Also supports uploading files and adding files from cloud providers such as Google Drive, Dropbox, Box and Microsoft OneDrive.

Each file component requires an app identifier (`uid`), which automatically creates a [corresponding app](https://docs.weavy.com/concepts#app) on your Weavy environment when the component is first initialized. The app identifier must correspond to the app of where you want to save the files. It's also recommended to specify a readable `name` of the files app, to get better readable notifications from the app.

> It's often useful to base the `uid` on something that identifies the location where the component is rendered.
> Typically you would use something like a product id, page id or path.

**Component Layout**

The component is displayed as a dropdown menu shown as a button. After the dropdown is a status icon button; shown when creating, uploading, attaching or modifying files in the app. The default height of the buttons is the icon size `1.5rem`/`24px` together with the set `--wy-padding` (which defaults to `0.5rem`/`8px`), which makes a total of `2.5rem`/`40px` per default. The width varies with the length of the text and whether the status is displayed or not.

The cloud file picker appears as a modal overlay centered in the `top layer`.

The file name selector when creating files appears as a modal input dialog centered in the `top layer`.

The status detail dialog renders in a sheet overlay centered in the `top layer`.

All the overlays does not occupy any visual layout space where they are placed in the DOM, they only render in the [top layer](https://developer.mozilla.org/en-US/docs/Glossary/Top_layer).

You can add additional styling using *CSS Custom Properties* and *CSS Shadow Parts* and further customization using *slots*.

**Used sub components:**

*   [`<wy-file-create>`](./components/wy-file-create)

### Examples

<figure class="example"><figcaption>Standard create new file button</figcaption>

Displays a file create button for the `test-files` files app with integrated file-creation, uploading and cloud file picker.

Specifying the app identifier (`uid`) is required, and automatically creates a [corresponding app](https://www.weavy.com/docs/concepts#app) on your Weavy environment when the component is first initialized.
It's recommended to specify a readable `name` of the files app, to get better readable notifications from the app.

```html
<wy-file-new uid="test-files" name="Test files"></wy-file-new>
```

</figure>

<figure class="example"><figcaption>Standard create new file button without cloud files</figcaption>

Displays a create new file button without the possibility to add files from cloud providers.

```html
<wy-file-new uid="test-files" features="-cloud_files"></wy-file-new>
```

</figure>

<figure class="example"><figcaption>Connect create new file button to `<wy-file>` view</figcaption>

Displays a create new file button that displays the added file in a [file view component](https://docs.weavy.com/reference/uikit/file).

```html
<wy-file-new id="file-picker" uid="test-files"></wy-file-new>
<wy-file id="file-view" uid="test-files"></wy-file>
<script>
const filePicker = document.getElementById("file-picker");
const fileView = document.getElementById("file-view");

filePicker.addEventListener("wy-action", (e) => {
if (e.detail.file) {
// Open the file in the file view;
fileView.fileId = e.detail.file.id;

// Optionally set edit mode
fileView.edit = e.detail.action === "edit";
}
})
```

</figure>

<figure class="example"><figcaption>Connect create new file button to `<wy-files>` list</figcaption>

Displays a create new file button that selects the added file in a [file list component](xref:uikit-files).

```html
<wy-file-new id="file-picker" uid="test-files"></wy-file-new>
<wy-files id="files-list" uid="test-files"></wy-files>
<script>
const filePicker = document.getElementById("file-picker");
const filesList = document.getElementById("files-list");

filePicker.addEventListener("wy-action", (e) => {
if (e.detail.file) {
// Select the file in the file list;
filesList.selectedId = e.detail.file.id;

// Ensure the file is visible in the list and scrolled to
filesList.highlightId = e.detail.file.id;

// Optionally open the file in preview
filesList.preview(e.detail.file.id, { edit: e.detail.action === "edit" })
}
})
```

</figure>

### Fields

| Name             | Type                                    | Default            | Description                                                                                                                                             | Inherited From     |
| ---------------- | --------------------------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
| `app`            | `AppType \| undefined`                  |                    | The current app data.                                                                                                                                   | WeavyAppComponent  |
| `apps`           | `AppListType \| undefined`              |                    | A list of data for all the apps specified by the \`uid\` property.                                                                                      | WeavyAppComponent  |
| `link`           | `LinkType \| undefined`                 |                    | Any provided link that should be loaded, shown and highlighted.                                                                                         | WeavyTypeComponent |
| `generateUid`    | `string \| null \| undefined`           |                    | Sets the uid property to a single uid with automatically appended user and agent name (where applicable).                                               | WeavyAppComponent  |
| `uid`            |                                         |                    | One or multiple unique app identifiers or app id:s. When a single uid is provided, the app gets automatically created on the server upon first request. | WeavyAppComponent  |
| `uids`           |                                         |                    | An array of parsed uids from the \`uid\` property.                                                                                                      | WeavyAppComponent  |
| `currentUid`     | `string \| number \| null \| undefined` | `undefined`        | The currently selected app uid when multiple \`uid\` is provided. Defaults to the first uid from the \`uid\` property.                                  | WeavyAppComponent  |
| `name`           | `string \| undefined`                   |                    | Optional display name for the app (used in notifications etc.)                                                                                          | WeavyAppComponent  |
| `directory`      | `string \| number \| undefined`         |                    | Optional directory identifier (\`id\` or \`name\`) for the app.                                                                                         | WeavyAppComponent  |
| `features`       | `string \| undefined`                   |                    | Config for enabling/disabling specific features in the weavy component.&#xA;Prefix a feature with minus to disable it.                                  | WeavyTypeComponent |
| `agent`          | `string \| undefined`                   |                    | The configured uid of the agent for the weavy component.                                                                                                | WeavyTypeComponent |
| `contextualData` | `string \| undefined`                   |                    | Contextual data for agents to reference. Provide descriptive data for optimal results.&#xA;&#xA;### Examples                                            | WeavyTypeComponent |
| `annotations`    | `AnnotationsAppearanceType`             | `"buttons-inline"` | Appearance of annotations.                                                                                                                              | WeavyTypeComponent |
| `enterToSend`    | `EnterToSend`                           | `"auto"`           | Enter-to-send keymap in the editor. \`"modifier"\` is \`Enter\` combined with \`Ctrl\` or \`Cmd\`.                                                      | WeavyTypeComponent |
| `reactions`      | `string`                                | `"😍 😎 😉 😜 👍"` | A space separated string of available reaction emojis in unicode.                                                                                       | WeavyTypeComponent |
| `weavy`          | `WeavyType \| undefined`                |                    | The consumed weavy context.                                                                                                                             | WeavyComponent     |
| `contextId`      | `ContextIdType`                         |                    | Contextual guid that is unique for the client context.                                                                                                  | WeavyComponent     |
| `user`           | `UserType \| undefined`                 |                    | The current user.                                                                                                                                       | WeavyComponent     |

### Methods

| Name               | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | Parameters                        | Return                    | Inherited From    |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- | ------------------------- | ----------------- |
| `createFile`       | Prompt for a filename and create a new file.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | `ext: string`                     |                           |                   |
| `uploadFile`       | Upload one or more files. If no file is provided, the user will be prompted to select a file.                                                                                                                                                                                                                                                                                                                                                                                                                                                             | `files: File[]`                   |                           |                   |
| `selectCloudFiles` | Request files via user input from a configured cloud file provider.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |                                   |                           |                   |
| `reset`            | Sets the component to it's initial state and resets the app state.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |                                   |                           | WeavyAppComponent |
| `subscribe`        | Subscribes or unsubscribes to notification updates from the app. Check \`.app.is\_subscribed\` to see current state.&#xA;&#xA;### Examples&#xA;&#xA;\<figure class="example">\<figcaption>Toggle app subscription\</figcaption>&#xA;&#xA;\`\`\`js&#xA;// Wait for app data&#xA;await myWeavyComponent.whenApp();&#xA;&#xA;// Get current app subscription state&#xA;const isSubscribed = myWeavyComponent.app.is\_subscribed;&#xA;&#xA;// Toggle the app subscription state&#xA;myWeavyComponent.subscribe(!isSubscribed);&#xA;\`\`\`&#xA;&#xA;\</figure> | `subscribe: boolean`              | `Promise<void>`           | WeavyAppComponent |
| `whenApp`          | Resolves when app data is available.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |                                   | `Promise<AppType>`        | WeavyAppComponent |
| `whenApps`         | Resolves when multiple apps is available.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |                                   | `Promise<AppType[]>`      | WeavyAppComponent |
| `whenUser`         | Resolves when current user data is available.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |                                   | `Promise<UserType>`       | WeavyComponent    |
| `whenWeavy`        | Resolves when a weavy context is available.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |                                   | `Promise<WeavyType>`      | WeavyComponent    |
| `whenReady`        | Resolves when the component has rendered first time.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |                                   | `Promise<WeavyComponent>` | WeavyComponent    |
| `dispatchAction`   | Dispatch a \`wy-action\` event on the component.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | `detail: WyActionEventDetailType` | `boolean`                 | WeavyComponent    |

### Events

| Name        | Type                | Description                                      | Inherited From |
| ----------- | ------------------- | ------------------------------------------------ | -------------- |
| `wy-app`    | `WyAppEventType`    | Fired whenever the app property changes.         |                |
| `wy-action` | `WyActionEventType` | Emitted when an action is performed on an embed. |                |
| `ready`     | `CustomEvent`       | When the component has rendered.                 | WeavyComponent |

### Attributes

| Name             | Field          | Inherited From     |
| ---------------- | -------------- | ------------------ |
| `link`           | link           | WeavyTypeComponent |
| `generateUid`    | generateUid    | WeavyAppComponent  |
| `uid`            | uid            | WeavyAppComponent  |
| `currentUid`     | currentUid     | WeavyAppComponent  |
| `name`           | name           | WeavyAppComponent  |
| `directory`      | directory      | WeavyAppComponent  |
| `features`       | features       | WeavyTypeComponent |
| `agent`          | agent          | WeavyTypeComponent |
| `contextualData` | contextualData | WeavyTypeComponent |
| `annotations`    | annotations    | WeavyTypeComponent |
| `enterToSend`    | enterToSend    | WeavyTypeComponent |
| `reactions`      | reactions      | WeavyTypeComponent |

### CSS Parts

| Name          | Description                                      |
| ------------- | ------------------------------------------------ |
| `wy-file-new` | Main container.                                  |
| `wy-dragging` | Modifier for the main layout when dragging over. |

### Slots

| Name      | Description                    |
| --------- | ------------------------------ |
| `actions` | Buttons placed in the toolbar. |

<details><summary>Private API</summary>

### Fields

| Name                         | Privacy   | Type                                                               | Default                                                                                                                   | Description                                                                                                                        | Inherited From     |
| ---------------------------- | --------- | ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
| `createFilesController`      | private   |                                                                    | `new CreateFilesController(this)`                                                                                         |                                                                                                                                    |                    |
| `fileCreateRef`              | private   |                                                                    |                                                                                                                           |                                                                                                                                    |                    |
| `appType`                    | protected | `AppTypeGuid \| UnknownAppType`                                    |                                                                                                                           | Any app type for the weavy component.                                                                                              | WeavyAppComponent  |
| `#uid`                       | private   | `string \| number \| null \| undefined`                            |                                                                                                                           |                                                                                                                                    | WeavyAppComponent  |
| `#uids`                      | private   | `Array<string \| number>`                                          | `[]`                                                                                                                      |                                                                                                                                    | WeavyAppComponent  |
| `_initialAppName`            | private   | `string \| undefined`                                              |                                                                                                                           |                                                                                                                                    | WeavyAppComponent  |
| `_appName`                   | private   | `string \| undefined`                                              |                                                                                                                           |                                                                                                                                    | WeavyAppComponent  |
| `#appUpsert`                 | private   |                                                                    | `new MutationController<AppType, Error, MutationAbortProps, void>(this)`                                                  | Upsert that makes sure the app exists. Kept beside the app query rather than inside it, see&#xA;\`getEnsureAppMutationOptions()\`. | WeavyAppComponent  |
| `#lastUpsertPayload`         | private   | `string \| undefined`                                              |                                                                                                                           | The payload of the last upsert this component sent, used to avoid repeating an identical write.                                    | WeavyAppComponent  |
| `#trackedApp`                | private   | `{ weavy: WeavyType; uid: string \| number } \| undefined`         |                                                                                                                           | The client and uid the app query is currently tracking, so identical options are not re-tracked.                                   | WeavyAppComponent  |
| `appSubscribeMutation`       | private   |                                                                    | `new MutationController< void, Error, MutateAppSubscribeProps, MutateAppSubscribeContextType >(this)`                     |                                                                                                                                    | WeavyAppComponent  |
| `#resolveApp`                | private   | `(app: AppType) => void \| undefined`                              |                                                                                                                           |                                                                                                                                    | WeavyAppComponent  |
| `#whenApp`                   | private   |                                                                    | `new Promise<AppType>((r) => { this.#resolveApp = r; })`                                                                  |                                                                                                                                    | WeavyAppComponent  |
| `#resolveApps`               | private   | `(apps: AppListType) => void \| undefined`                         |                                                                                                                           |                                                                                                                                    | WeavyAppComponent  |
| `#whenApps`                  | private   |                                                                    | `new Promise<AppListType>((r) => { this.#resolveApps = r; })`                                                             |                                                                                                                                    | WeavyAppComponent  |
| `#appQuery`                  | private   |                                                                    | `new QueryController<AppType>(this)`                                                                                      |                                                                                                                                    | WeavyAppComponent  |
| `#multipleAppsQueryMap`      | private   |                                                                    | `new Map<string \| number, QueryController<AppType>>()`                                                                   |                                                                                                                                    | WeavyAppComponent  |
| `#appsQuery`                 | private   |                                                                    |                                                                                                                           |                                                                                                                                    | WeavyAppComponent  |
| `visibilityElement`          | protected | `Element \| undefined`                                             |                                                                                                                           | Element to match visibility on.                                                                                                    | WeavyTypeComponent |
| `appTypes`                   | protected | `AppTypeGuid[]`                                                    |                                                                                                                           | Any app types handled by the component.                                                                                            | WeavyTypeComponent |
| `#data`                      | private   | `ContextDataType[] \| undefined`                                   |                                                                                                                           |                                                                                                                                    | WeavyTypeComponent |
| `#annotations`               | private   | `WeavyComponentSettingProps["annotations"] \| undefined`           |                                                                                                                           |                                                                                                                                    | WeavyTypeComponent |
| `#enterToSend`               | private   | `EnterToSend \| undefined`                                         |                                                                                                                           |                                                                                                                                    | WeavyTypeComponent |
| `#reactions`                 | private   | `WeavyComponentSettingProps["reactions"] \| undefined`             |                                                                                                                           |                                                                                                                                    | WeavyTypeComponent |
| `#resolveContextDataBlobs`   | private   | `(blobs: ContextDataBlobsType) => void \| undefined`               |                                                                                                                           |                                                                                                                                    | WeavyTypeComponent |
| `#whenContextDataBlobs`      | private   |                                                                    | `new Promise<ContextDataBlobsType>((r) => { this.#resolveContextDataBlobs = r; })`                                        |                                                                                                                                    | WeavyTypeComponent |
| `#resolveComponentFeatures`  | private   | `(componentFeatures: ComponentFeaturePolicy) => void \| undefined` |                                                                                                                           |                                                                                                                                    | WeavyTypeComponent |
| `#whenComponentFeatures`     | private   |                                                                    | `new Promise<ComponentFeaturePolicy>((r) => { this.#resolveComponentFeatures = r; })`                                     |                                                                                                                                    | WeavyTypeComponent |
| `#resolveLink`               | private   | `(link: LinkType) => void \| undefined`                            |                                                                                                                           |                                                                                                                                    | WeavyTypeComponent |
| `#whenLink`                  | private   |                                                                    | `new Promise<LinkType>((r) => { this.#resolveLink = r; })`                                                                |                                                                                                                                    | WeavyTypeComponent |
| `#resolveSettings`           | private   | `(settings: WeavyComponentSettingsType) => void \| undefined`      |                                                                                                                           |                                                                                                                                    | WeavyTypeComponent |
| `#whenSettings`              | private   |                                                                    | `new Promise<WeavyComponentSettingsType>((r) => { this.#resolveSettings = r; })`                                          |                                                                                                                                    | WeavyTypeComponent |
| `#contextDataRefs`           | private   | `Map<ContextDataType, DataRefType>`                                | `new Map()`                                                                                                               |                                                                                                                                    | WeavyTypeComponent |
| `#uploadContextDataMutation` | private   |                                                                    | `new MutationController<BlobType, Error, MutateFileProps, FileMutationContextType>(this)`                                 |                                                                                                                                    | WeavyTypeComponent |
| `#mutatingContextData`       | private   |                                                                    | `new MutationStateController< BlobType \| FileType \| EmbedType, Error, MutateFileProps, FileMutationContextType >(this)` |                                                                                                                                    | WeavyTypeComponent |
| `#resolveContextId`          | private   | `(contextId: ContextIdType) => void \| undefined`                  |                                                                                                                           |                                                                                                                                    | WeavyComponent     |
| `#whenContextId`             | private   |                                                                    | `new Promise<ContextIdType>((r) => { this.#resolveContextId = r; })`                                                      |                                                                                                                                    | WeavyComponent     |
| `#resolveUser`               | private   | `(user: UserType) => void \| undefined`                            |                                                                                                                           |                                                                                                                                    | WeavyComponent     |
| `#whenUser`                  | private   |                                                                    | `new Promise<UserType>((r) => { this.#resolveUser = r; })`                                                                |                                                                                                                                    | WeavyComponent     |
| `#resolveWeavy`              | private   | `(weavy: WeavyType) => void \| undefined`                          |                                                                                                                           |                                                                                                                                    | WeavyComponent     |
| `#whenWeavy`                 | private   |                                                                    | `new Promise<WeavyType>((r) => { this.#resolveWeavy = r; })`                                                              |                                                                                                                                    | WeavyComponent     |
| `#resolveReady`              | private   | `(component: WeavyComponent) => void \| undefined`                 |                                                                                                                           |                                                                                                                                    | WeavyComponent     |
| `#whenReady`                 | private   |                                                                    | `new Promise<WeavyComponent>((r) => { this.#resolveReady = r; })`                                                         |                                                                                                                                    | WeavyComponent     |
| `#userQuery`                 | private   |                                                                    | `new QueryController<UserType>(this)`                                                                                     |                                                                                                                                    | WeavyComponent     |

### Methods

| Name                 | Privacy   | Description                                                                                                            | Parameters                    | Return | Inherited From     |
| -------------------- | --------- | ---------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ------ | ------------------ |
| `setLink`            | protected | Sets the link value to a new link                                                                                      | `link: LinkType \| undefined` |        | WeavyTypeComponent |
| `provideStorageLink` | protected | Shares a link with other blocks that may consume it automatically in other tabs or windows or when reloading the page. | `link: LinkType`              |        | WeavyTypeComponent |

</details>

<hr/>

## Exports

| Kind                        | Name          | Declaration | Module              | Package |
| --------------------------- | ------------- | ----------- | ------------------- | ------- |
| `js`                        | `WyFileNew`   | WyFileNew   | lib/wy-file-new\.ts |         |
| `custom-element-definition` | `wy-file-new` | WyFileNew   | lib/wy-file-new\.ts |         |
