<script setup lang="ts">
import { ref, watchEffect } from 'vue'

import Alert from '~/components/ui/Alert.vue'
import Button from '~/components/ui/Button.vue'
import Modal from '~/components/ui/Modal.vue'
import Layout from '~/components/ui/Layout.vue'

const isOpen = ref(false)
const isOpen2 = ref(false)
const isOpen3 = ref(false)

const alertOpen = ref(true)
watchEffect(() => {
  if (isOpen3.value === false) {
    alertOpen.value = true
    }
})

const isOpen4 = ref(false)
const isOpen5 = ref(false)
const isOpen6 = ref(false)

</script>

# Modal

| Prop      | Data type         | Required? | Default | Description                        |
| --------- | ----------------- | --------- | ------- | ---------------------------------- |
| `title`   | `string`          | Yes       |         | The modal title                    |
| `v-model` | `true` \| `false` | No        |         | Whether the modal is isOpen or not |

<Layout flex>

```vue-html
<Button @click="isOpen = true">
  Open modal
</Button>

<Modal v-model="isOpen" title="My modal">
  Modal content
</Modal>
```

<div class="preview">
<Button @click="isOpen = true">
  Open modal
</Button>
<Modal v-model="isOpen" title="My modal">
  Modal content
</Modal>
</div>

</Layout>

## Modal actions

Use the `#actions` slot to add actions to a modal. Actions typically take the form of [buttons](./button).

Make sure to add `autofocus` to the preferred button.

<Layout flex>

```vue-html
<Button @click="isOpen = true">
  Open modal
</Button>

<Modal v-model="isOpen" title="My modal">
  Modal content

  <template #actions>
    <Button @click="isOpen = false">
      Cancel
    </Button>

    <Button autofocus @click="isOpen = false">
      Ok
    </Button>
  </template>
</Modal>
```

<div class="preview">
<Button @click="isOpen2 = true">
  Open modal
</Button>
<Modal v-model="isOpen2" title="My modal">
  Modal content
  <template #actions>
    <Button @click="isOpen2 = false">
      Cancel
    </Button>
    <Button autofocus @click="isOpen2 = false">
      Ok
    </Button>
  </template>
</Modal>
</div>

</Layout>

### Confirm a dangerous action

Note that confirmation dialogs interrupt the user's workflow. Consider adding a recovery functionality such as "undo" instead.

::: tip Read more about designing user experiences around dangerous actions:

- [How to use visual signals and spacing to differentiate between benign and dangerous options](https://www.nngroup.com/articles/proximity-consequential-options/)

  > If you need to implement dangerous actions, make sure to place them apart from other actions to prevent accidental clicks. Add contextual hints and information so that the user understands the consequences of the action.

- [How to design a confirmation dialog](https://www.nngroup.com/articles/confirmation-dialog/)
  > 1. Let the user confirm potentially destructive actions
  > 2. Do not use confirmation dialogs for routine tasks
  > 3. Be specific about the action and its potential consequences
  > 4. Label the response buttons with their result: "Delete my account" instead of "Yes"
  > 5. Make sure to give the user all information they need to decide

:::

```vue-html
<Button @click="isOpen = true" destructive>
  Delete my account ...
</Button>

<Modal v-model="isOpen" title="Delete account?">
  <template #alert>
    <Alert red>
1 082 music files that you uploaded will be deleted.<br />
7 879 items in your collections will be unlinked.
    </Alert>
  </template>
Do you want to delete your account forever?

You will not be able to restore your account.
  <template #actions>
    <Button autofocus @click="isOpen = false" >
      Keep my account
    </Button>
    <Button destructive @click="isOpen = false">
      I understand. Delete my account now!
    </Button>
  </template>
</Modal>
```

<Button @click="isOpen6 = true" destructive>
Delete my account ...
</Button>
<Modal v-model="isOpen6" title="Delete account?">
<template #alert>
<Alert red>
1 082 music files that you uploaded will be deleted.<br />
7 879 items in your collections will be unlinked.
</Alert>
</template>
Do you want to delete your account forever?

You will not be able to restore your account.
<template #actions>
<Button autofocus @click="isOpen6 = false">
Keep my account
</Button>
<Button destructive @click="isOpen6 = false">
I understand. Delete my account now!
</Button>
</template>
</Modal>

## Nested modals

You can nest modals to allow users to isOpen a modal from inside another modal. This can be useful when creating a multi-step workflow.

<Layout flex>

```vue-html
<Button @click="isOpen = true">
  Open modal
</Button>

<Modal v-model="isOpen" title="My modal">
  <Modal v-model="isOpenNested" title="My modal">
    Nested modal content
  </Modal>

  <Button autofocus @click="isOpenNested = true">
    Open nested modal
  </Button>
</Modal>
```

<div class="preview">
<Button @click="isOpen4 = true">
  Open modal
</Button>
<Modal v-model="isOpen4" title="My modal">
  <Modal v-model="isOpen5" title="My modal">
    Nested modal content
  </Modal>
  <Button autofocus @click="isOpen5 = true">
    Open nested modal
  </Button>
</Modal>
</div>

</Layout>

## Alert inside modal

You can nest [Funkwhale alerts](./alert) to visually highlight content within the modal.

<Layout flex>

```vue-html
<Button @click="isOpen = true">
  Open modal
</Button>

<Modal v-model="isOpen" title="My modal">
  Modal content

  <template #alert v-if="alertOpen">
    <fw-alert>
      Alert content

      <template #actions>
        <Button autofocus @click="alertOpen = false">Close alert</Button>
      </template>
    </fw-alert>
  </template>
</Modal>
```

<div class="preview">
<Button @click="isOpen3 = true">
  Open modal
</Button>
<Modal v-model="isOpen3" title="My modal">
  Modal content
  <template #alert v-if="alertOpen">
    <Alert blue>
      Alert content
      <template #actions>
        <Button autofocus @click="alertOpen = false">Close alert</Button>
      </template>
    </Alert>
  </template>
  <template #actions>
    <Button @click="isOpen3 = false" color="secondary">
      Cancel
    </Button>
    <Button primary @click="isOpen3 = false">
      Ok
    </Button>
  </template>
</Modal>
</div>

</Layout>
