# AvalynxScrollToTop

[![npm version](https://jbs-newmedia.de/badge/npm/avalynx-scrolltotop/version.svg)](https://www.npmjs.com/package/avalynx-scrolltotop)
[![npm downloads](https://jbs-newmedia.de/badge/npm/avalynx-scrolltotop/download.svg)](https://www.npmjs.com/package/avalynx-scrolltotop)
[![Composer version](https://jbs-newmedia.de/badge/composer/avalynx/avalynx-scrolltotop/version.svg)](https://packagist.org/packages/avalynx/avalynx-scrolltotop)
[![Composer downloads](https://jbs-newmedia.de/badge/composer/avalynx/avalynx-scrolltotop/download.svg)](https://packagist.org/packages/avalynx/avalynx-scrolltotop)
[![jsDelivr](https://jbs-newmedia.de/badge/jsdelivr/avalynx-scrolltotop/hits.svg)](https://www.jsdelivr.com/package/npm/avalynx-scrolltotop)
[![License](https://jbs-newmedia.de/badge/npm/avalynx-scrolltotop/license.svg)](LICENSE)
[![Tests](https://jbs-newmedia.de/badge/github/avalynx/avalynx-scrolltotop/tests.svg)](https://github.com/avalynx/avalynx-scrolltotop/actions/workflows/tests.yml)
[![codecov](https://jbs-newmedia.de/badge/codecov/avalynx/avalynx-scrolltotop/coverage.svg)](https://codecov.io/gh/avalynx/avalynx-scrolltotop)
[![GitHub stars](https://jbs-newmedia.de/badge/github/avalynx/avalynx-scrolltotop/stars.svg)](https://github.com/avalynx/avalynx-scrolltotop)

AvalynxScrollToTop is a simple, lightweight, and customizable scroll-to-top button component. It is based on Bootstrap (version 5.3 or higher) and does not require any other framework dependencies.

## Features

- **Flexible Positioning**: Place the button at `bottom-right`, `bottom-left`, `top-right` or `top-left`.
- **Container Support**: Attach the button to the page (`position: fixed`) or to any scrollable container (`position: sticky`).
- **Smooth Scrolling**: Scrolls back to the top with native smooth scroll behavior (can be disabled).
- **Custom Icons**: Use the default SVG arrow or any icon markup (e.g. Font Awesome). The markup is sanitized to prevent XSS.
- **Optional Theme**: A square, sharp-cornered theme stylesheet that takes its colors from Bootstrap variables and supports light and dark mode.
- **Accessible**: Configurable `aria-label` and a visible focus ring.

## Examples

Here is an overview of various use cases for AvalynxScrollToTop:

* [Overview](https://avalynx-scrolltotop.jbs-newmedia.de/examples/index.html)
* [Simple scrolltotop](https://avalynx-scrolltotop.jbs-newmedia.de/examples/simple-scrolltotop.html)
* [Scrolltotop with options](https://avalynx-scrolltotop.jbs-newmedia.de/examples/scrolltotop-with-options.html)
* [Scrolltotop in container](https://avalynx-scrolltotop.jbs-newmedia.de/examples/scrolltotop-in-container.html)
* [Scrolltotop in every position](https://avalynx-scrolltotop.jbs-newmedia.de/examples/scrolltotop-every-position.html)
* [Scrolltotop theme](https://avalynx-scrolltotop.jbs-newmedia.de/examples/scrolltotop-theme.html)

## Installation

To use AvalynxScrollToTop in your project, you can directly include it in your HTML file. Ensure you have Bootstrap 5.3 or higher included in your project for AvalynxScrollToTop to work correctly.

First, include Bootstrap:

```html
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3/dist/js/bootstrap.bundle.min.js"></script>
```

Then, include AvalynxScrollToTop:

```html
<link href="path/to/avalynx-scrolltotop.css" rel="stylesheet">
<script src="path/to/avalynx-scrolltotop.js"></script>
```

Replace `path/to/avalynx-scrolltotop.js` and `path/to/avalynx-scrolltotop.css` with the actual path to the files in your project. The stylesheet is the optional theme; without it, the button uses the Bootstrap classes from the `className` option.

## Installation via jsDelivr ([Link](https://cdn.jsdelivr.net/npm/avalynx-scrolltotop/))

AvalynxScrollToTop is also available via [jsDelivr](https://www.jsdelivr.com/). You can include it in your project like this:

```html
<link href="https://cdn.jsdelivr.net/npm/avalynx-scrolltotop@1.0.2/dist/css/avalynx-scrolltotop.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/avalynx-scrolltotop@1.0.2/dist/js/avalynx-scrolltotop.js"></script>
```

Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxScrollToTop displays correctly.

## Installation via NPM ([Link](https://www.npmjs.com/package/avalynx-scrolltotop))

AvalynxScrollToTop is also available as a npm package. You can add it to your project with the following command:

```bash
npm install avalynx-scrolltotop
```

After installing, you can import AvalynxScrollToTop into your JavaScript file like this:

```javascript
import { AvalynxScrollToTop } from 'avalynx-scrolltotop';
import 'avalynx-scrolltotop/dist/css/avalynx-scrolltotop.css';
```

Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxScrollToTop displays correctly.

## Installation via Symfony AssetMapper

```bash
php bin/console importmap:require avalynx-scrolltotop
```

After installing, you can import AvalynxScrollToTop into your JavaScript file like this:

```javascript
import { AvalynxScrollToTop } from 'avalynx-scrolltotop';
import 'avalynx-scrolltotop/dist/css/avalynx-scrolltotop.css';
```

Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxScrollToTop displays correctly.

## Installation via Symfony AssetComposer

More information about the Symfony AssetComposer Bundle can be found [here](https://github.com/jbsnewmedia/asset-composer-bundle).

```twig
{% do addAssetComposer('avalynx/avalynx-scrolltotop/dist/css/avalynx-scrolltotop.css') %}
{% do addAssetComposer('avalynx/avalynx-scrolltotop/dist/js/avalynx-scrolltotop.js') %}
```

Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxScrollToTop displays correctly.

## Installation via Composer ([Link](https://packagist.org/packages/avalynx/avalynx-scrolltotop))

AvalynxScrollToTop is also available as a Composer package. You can add it to your project with the following command:

```bash
composer require avalynx/avalynx-scrolltotop
```

After installing, you can import AvalynxScrollToTop into your HTML file like this:

```html
<link href="vendor/avalynx/avalynx-scrolltotop/dist/css/avalynx-scrolltotop.css" rel="stylesheet">
<script src="vendor/avalynx/avalynx-scrolltotop/dist/js/avalynx-scrolltotop.js"></script>
```

Make sure to also include Bootstrap's JS/CSS in your project to ensure AvalynxScrollToTop displays correctly.

## Usage

To create a scroll-to-top button for the page, simply instantiate a new `AvalynxScrollToTop` object:

```javascript
new AvalynxScrollToTop();
```

With custom options:

```javascript
new AvalynxScrollToTop('body', {
    scrollTrigger: 200,
    position: 'bottom-left',
    className: 'btn btn-dark shadow-sm',
    smoothScroll: true
});
```

For a scrollable container:

```javascript
new AvalynxScrollToTop('#my-scrollable-container', {
    scrollTrigger: 50,
    position: 'bottom-right'
});
```

## Theme

The optional stylesheet `avalynx-scrolltotop.css` renders a square, sharp-cornered button. Its colors come from Bootstrap variables (`--bs-primary`, `--bs-btn-*`) and can be overridden with real values:

```css
button.avalynx-scrolltotop {
    --avalynx-scrolltotop-color: #ffffff;
    --avalynx-scrolltotop-bg: #a42636;
    --avalynx-scrolltotop-bg-hover: #8b202e;
    --avalynx-scrolltotop-bg-active: #831e2b;
    --avalynx-scrolltotop-focus-ring: rgba(164, 38, 54, 0.35);
}
```

## Options

AvalynxScrollToTop allows the following options for customization:

- `selector`: (string) The CSS selector for the container element (default: `'body'`).
- `options`: An object containing the following keys:
    - `scrollTrigger`: (number) The scroll position (in pixels) that triggers the button visibility (default: `100`).
    - `smoothScroll`: (boolean) Whether scrolling back to the top should be smooth (default: `true`).
    - `position`: (string) The position of the button. One of `bottom-right`, `bottom-left`, `top-right`, `top-left` (default: `'bottom-right'`).
    - `className`: (string) Additional CSS classes for the button (default: `'btn btn-primary'`).
    - `icon`: (string) The icon markup. Scripts, event-handler attributes, `javascript:` URLs and embedded/browsing elements are stripped (default: SVG arrow-up).
    - `ariaLabel`: (string) The aria-label of the button (default: `'Scroll to top'`).
    - `zIndex`: (string) The z-index of the button (default: `'1000'`).

## Contributing

Contributions are welcome! If you'd like to contribute, please fork the repository and submit a pull request with your changes or improvements. We're looking for contributions in the following areas:

- Bug fixes
- Feature enhancements
- Documentation improvements

Before submitting your pull request, please ensure your changes are well-documented and follow the existing coding style of the project.

## License

AvalynxScrollToTop is open-sourced software licensed under the [MIT license](LICENSE).

## Contact

If you have any questions, feature requests, or issues, please open an issue on our [GitHub repository](https://github.com/avalynx/avalynx-scrolltotop/issues) or submit a pull request.

Thank you for considering AvalynxScrollToTop for your project!
