Skip to content

Commit dd91bf3

Browse files
committed
docs: added some usage notes
1 parent b57a084 commit dd91bf3

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

package/README.md

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# vue-simple-portal
22

3-
NOT PUBLISHED YET
4-
53
<!-- markdownlint-disable MD024 MD025 MD033 -->
64

75
## What this is
@@ -15,14 +13,14 @@ Its main usecase are components/elements that need to be positioned absolutely r
1513
* Alerts/notifications
1614
* Toasts
1715

18-
## Usage
16+
## Usage Example
1917

2018
Minimal example:
2119

2220
```html
2321
<body>
2422
<script src="https://unkpg.com/vue/dist/vue.js"></script>
25-
<script src="https://unkpg.com/vue-simple-portal"></script>
23+
<script src="https://unkpg.com/@linusborg/vue-simple-portal"></script>
2624
<div id="app">
2725
<-- your Vue app mounts to this element -->
2826
</div>
@@ -89,9 +87,6 @@ So I experimented a little and came up with this library here, which solves thes
8987

9088
### NPM / Yarn
9189

92-
> Since this package hasn't been published yet, this won't work.
93-
> It will be released soon.
94-
9590
```bash
9691
npm install -D @linusborg/vue-simple-portal
9792
# or
@@ -105,7 +100,7 @@ This will make the `<portal>` component available globally, but also make the po
105100
```javascript
106101
// main.js
107102
import Vue from 'vue'
108-
import VuePortal from '@inusborg/vue-simple-portal'
103+
import VuePortal from '@linusborg/vue-simple-portal'
109104

110105
Vue.use(VuePortal, {
111106
name: 'portal', // optional, use to rename component
@@ -116,14 +111,14 @@ Vue.use(VuePortal, {
116111

117112
```javascript
118113
// in a component definition
119-
import { Portal } from '@inusborg/vue-simple-portal'
114+
import { Portal } from '@linusborg/vue-simple-portal'
120115
export default {
121116
name 'MyComponent',
122117
components: {
123118
Portal
124119
}
125120
}
126-
````
121+
```
127122

128123
### Browser
129124

@@ -138,6 +133,47 @@ Just include it with a script tag *after* Vue itself
138133

139134
The plugin will automatically register the `<portal>` component globally.
140135

136+
## Usage Notes
137+
138+
`<portal>` works out of the box without setting any props. By default, it will:
139+
140+
1. Create a randomized id selector (once)
141+
2. Append a `<div>` with that id to the `<body>` (once)
142+
3. Append any `<portal>`'s slot content as a small, transparent component to that `<div>`
143+
144+
Which means the content is not an almost direct decendant of `<body>`, and can safely and reliably be positioned absolutely etc.
145+
146+
So it's even easier than in the Usage Example from above:
147+
148+
```html
149+
<portal>
150+
<p>This will be mounted as a child element
151+
of the auto-generated <div> instead of
152+
somewhere inside the child tree of <div id="app">
153+
</portal>
154+
```
155+
156+
### Customizing the selector.
157+
158+
As shown in the initial Usage Example, you can use the `selector` prop to append to an element of your choosing.
159+
160+
If you are tired of specifying the selector over and over again, you can set it as the default selector, overwriting the randomly generated one:
161+
162+
```javascript
163+
Vue.use(VuePortal, {
164+
selector: '#your-target',
165+
})
166+
```
167+
168+
If you don't want to install the plugin globally, you can use the `setSelector` helper:
169+
170+
```javascript
171+
import { setSelector } from '@œlinusborg/vue-simple-portal'
172+
setSelector('#your-target')
173+
```
174+
175+
176+
141177
## Props
142178

143179
This library aims to do one thing only, and do it well: move stuff to the end of the document for things like modals. But it still gives the developer a few props to influence how exactly that happens:
@@ -248,7 +284,7 @@ As a last resort you can always use a Timeout if yu know the duration of the lea
248284

249285
If the slot content of the `<portal>` contains components, they will show up as children of the `<portal>` in the devtools, even though their root elements were mounted/moved to the target element, outside of the current component tree.
250286

251-
### Target elements inside of your Vue app
287+
### Targeting elements inside of your Vue app
252288

253289
The general advise is to only mount to elements *outside* of your Vue app, as that's the prime use case of this library. If you need to mount to locations *inside of* your app, consider using [portal-vue](https://portal-vue.linusb.org) instead.
254290

0 commit comments

Comments
 (0)