Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic README #31

Merged
merged 10 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pnpm-lock.yaml
package-lock.json
yarn.lock
/coverage
**/*.mdx
**/*.md
87 changes: 59 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,80 @@

A Floating UI wrapper for Svelte.

## Attribution
## Installation

This project is maintained by [Hugo Korte](https://github.com/Hugos68), [Skeleton Labs](https://www.skeletonlabs.co/), and the [Svelte](https://svelte.dev/) community. Based on the amazing work by [Floating UI](https://github.com/floating-ui/floating-ui).
```bash
npm install @skeletonlabs/floating-ui-svelte
# pnpm install @skeletonlabs/floating-ui-svelte
# yarn install @skeletonlabs/floating-ui-svelte
# bun install @skeletonlabs/floating-ui-svelte
```

## Contributing
## Usage

### Developing
Import the desired hook or component from floating-ui-svelte. [View each example](https://floating-ui-svelte.vercel.app/) for additional guidance.

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
```js
import { useFloating, type UseFloatingOptions } from '@skeletonlabs/floating-ui-svelte';

```bash
npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
const options: UseFloatingOptions = { /* ... */ };
const floating = useFloating(options);
endigo9740 marked this conversation as resolved.
Show resolved Hide resolved
```

Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app.
## API

### Building
### useFloating

To build your library:
The `useFloating` Svelte hook acts as a controller for all other Floating UI Svelte features. It handles positioning your floating elements (tooltips, popovers, etc.) relative to an anchored element. Automatically calculates the best placement and updates it as needed, providing access to properties for position and style.

```bash
npm run package
```
#### Usage

To create a production version of your showcase app:
```html
<script>
import { useFloating } from '@skeletonlabs/floating-ui-svelte';

```bash
npm run build
```
const elements = $state({ reference: null, floating: null });
const floating = useFloating({ elements });
</script>

You can preview the production build with `npm run preview`.
<div bind:this="{elements.reference}">Reference</div>
<div bind:this="{elements.floating}" styles="{floating.floatingStyles}">Floating</div>
```

> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
> [!WARNING]
> Destructured variables are not supported as this would break reactivity.

#### Options
Hugos68 marked this conversation as resolved.
Show resolved Hide resolved

| Property | Description | Type | Default Value |
| -------- | ----------- | ---- | ------------- |
| open | Represents the open/close state of the floating element. | boolean | true |
| onOpenChange | Event handler that can be invoked whenever the open state changes. | (open: boolean, event?: Event, reason?: OpenChangeReason) => void | - |
| placement | Where to place the floating element relative to its reference element. | Placement | 'bottom' |
| strategy | The type of CSS position property to use. | Strategy | 'absolute' |
| middleware | Supports all [Floating UI middleware](https://floating-ui.com/docs/middleware). | Array<Middleware \| undefined \| null \| false> | undefined |
| transform | Whether to use `transform` instead of `top` and `left` styles to position the floating element (`floatingStyles`). | boolean | true |
| elements | The reference and floating elements. | FloatingElements | - |
| whileElementsMounted | Callback to handle mounting/unmounting of the elements. | (reference: ReferenceElement, floating: FloatingElement, update: () => void) => () => void | - |

#### Return Values

| Property | Description | Type |
| -------- | ----------- | ---- |
| x | The x-coord of the floating element. | number |
| y | The y-coord of the floating element. | number |
| placement | The stateful placement, which can be different from the initial `placement` passed as options. | Placement |
| strategy | The stateful strategy, which can be different from the initial `strategy` passed as options. | Strategy |
| middlewareData | Additional data from middleware. | MiddlewareData |
| isPositioned | The boolean that let you know if the floating element has been positioned. | boolean |
| floatingStyles | CSS styles to apply to the floating element to position it. | string |
| update | The function to update floating position manually. | () => void |
| context | Context object containing internal logic to alter the behavior of the floating element. | FloatingContext |

### Publishing
## Attribution

Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)).
Based on [Floating UI](https://github.com/floating-ui/floating-ui) and [Floating UI React](https://floating-ui.com/docs/react). Maintained by [Hugo Korte](https://github.com/Hugos68), [Skeleton Labs](https://www.skeletonlabs.co/), and the [Svelte community](https://svelte.dev/).

To publish your library to [npm](https://www.npmjs.com):
## License

```bash
npm publish
```
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
Loading