diff --git a/docs/v2/esbuild.mjs b/docs/v2/esbuild.mjs index 3e018c1..4289cd7 100644 --- a/docs/v2/esbuild.mjs +++ b/docs/v2/esbuild.mjs @@ -7,7 +7,7 @@ const minify = processArgs.includes('-p'); const define = {}; await esbuild({ - entryPoints: glob.sync('./src/**/*.{ts,tsx,js,jsx}'), + entryPoints: glob.sync('./src/**/*.{ts,tsx,js,jsx,md,mdx}'), outdir: './next-app/src', outbase: 'src', platform: 'node', @@ -23,8 +23,8 @@ await esbuild({ '.ico': 'copy', '.jpeg': 'copy', '.jpg': 'copy', - '.md': 'jsx', - '.mdx': 'jsx', + '.md': 'copy', + '.mdx': 'copy', '.mp3': 'copy', '.mp4': 'copy', '.ogg': 'copy', diff --git a/docs/v2/globals.d.ts b/docs/v2/globals.d.ts index cb86d3c..51ee558 100644 --- a/docs/v2/globals.d.ts +++ b/docs/v2/globals.d.ts @@ -1,31 +1,33 @@ -declare module '*.ico' { - const asset: string; - export default asset; -} declare module '*.aac' { const asset: string; export default asset; } -declare module '*.avif' { - const asset: string; - export default asset; -} + declare module '*.css' { const asset: string; export default asset; } + +declare module '*.scss' { + const asset: string; + export default asset; +} + +declare module '*.sass' { + const asset: string; + export default asset; +} + declare module '*.eot' { const asset: string; export default asset; } + declare module '*.flac' { const asset: string; export default asset; } -declare module '*.gif' { - const asset: string; - export default asset; -} + declare module '*.gql' { const asset: string; export default asset; @@ -34,22 +36,7 @@ declare module '*.graphql' { const asset: string; export default asset; } -declare module '*.jpeg' { - const asset: string; - export default asset; -} -declare module '*.jpg' { - const asset: string; - export default asset; -} -declare module '*.md' { - import type { ComponentType as MdComponentType } from 'react'; - export const attributes: any; - export const filename: string; - const Component: MdComponentType; - export default Component; -} declare module '*.mdx' { import type { ComponentType as MdxComponentType } from 'react'; @@ -58,34 +45,32 @@ declare module '*.mdx' { const Component: MdxComponentType; export default Component; } + declare module '*.mp3' { const asset: string; export default asset; } + declare module '*.mp4' { const asset: string; export default asset; } + declare module '*.ogg' { const asset: string; export default asset; } + declare module '*.otf' { const asset: string; export default asset; } -declare module '*.png' { - const asset: string; - export default asset; -} + declare module '*.sql' { const asset: string; export default asset; } -declare module '*.svg' { - const asset: string; - export default asset; -} + declare module '*.ttf' { const asset: string; export default asset; @@ -98,22 +83,22 @@ declare module '*.webm' { const asset: string; export default asset; } -declare module '*.webp' { - const asset: string; - export default asset; -} + declare module '*.woff' { const asset: string; export default asset; } + declare module '*.woff2' { const asset: string; export default asset; } + declare module '*.webmanifest' { const asset: string; export default asset; } + declare module '*.zip' { const asset: string; export default asset; diff --git a/docs/v2/next-app/next.config.js b/docs/v2/next-app/next.config.js deleted file mode 100644 index 83ebfd7..0000000 --- a/docs/v2/next-app/next.config.js +++ /dev/null @@ -1,10 +0,0 @@ -/** @type {import('next').NextConfig} */ -const nextConfig = { - reactStrictMode: true, - swcMinify: true, - images: { - unoptimized: true, - }, -}; - -module.exports = nextConfig; diff --git a/docs/v2/next-app/next.config.mjs b/docs/v2/next-app/next.config.mjs new file mode 100644 index 0000000..52714be --- /dev/null +++ b/docs/v2/next-app/next.config.mjs @@ -0,0 +1,34 @@ +import mdx from '@next/mdx'; +import rehypePrettyCode from 'rehype-pretty-code'; +import rehypeSlug from 'rehype-slug'; +import remarkGfm from 'remark-gfm'; + +/** @type {import('next').NextConfig} */ +const withMDX = mdx({ + extension: /\.mdx?$/, + options: { + remarkPlugins: [remarkGfm], + + rehypePlugins: [ + rehypeSlug, + [ + rehypePrettyCode, + { + theme: 'solarized-light', + }, + ], + ], + providerImportSource: '@mdx-js/react', + }, +}); + +const nextConfig = { + reactStrictMode: true, + swcMinify: true, + images: { + unoptimized: true, + }, + pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'], +}; + +export default withMDX(nextConfig); diff --git a/docs/v2/package.json b/docs/v2/package.json index 70fb3a5..ea8cf01 100644 --- a/docs/v2/package.json +++ b/docs/v2/package.json @@ -6,6 +6,10 @@ "start": "node esbuild.mjs && cd next-app && next start" }, "dependencies": { + "@mdx-js/loader": "^2.1.5", + "@mdx-js/react": "^2.1.5", + "@next/mdx": "^12.3.1", + "classnames": "^2.3.2", "next": "12.3.1", "react": "18.2.0", "react-dom": "18.2.0" @@ -31,7 +35,11 @@ "eslint-plugin-tailwindcss": "^3.6.2", "fast-glob": "^3.2.12", "prettier": "2.7.1", + "rehype-pretty-code": "^0.4.0", + "rehype-slug": "^5.0.1", + "remark-gfm": "^3.0.1", "sass": "^1.55.0", + "shiki": "^0.11.1", "tailwindcss": "^3.1.8", "typescript": "4.8.3", "wait-on": "^6.0.1" diff --git a/docs/v2/src/assets/css/tailwind.css b/docs/v2/src/assets/css/tailwind.css index b5c61c9..13a4772 100644 --- a/docs/v2/src/assets/css/tailwind.css +++ b/docs/v2/src/assets/css/tailwind.css @@ -1,3 +1,6 @@ +.default\:fill-current { + @apply fill-current; +} @tailwind base; @tailwind components; @tailwind utilities; diff --git a/docs/v2/src/assets/icon/github.svg b/docs/v2/src/assets/icon/github.svg new file mode 100644 index 0000000..6b7b75d --- /dev/null +++ b/docs/v2/src/assets/icon/github.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/docs/v2/src/assets/img/adminlte-logo.png b/docs/v2/src/assets/img/adminlte-logo.png new file mode 100644 index 0000000..412bdab Binary files /dev/null and b/docs/v2/src/assets/img/adminlte-logo.png differ diff --git a/docs/v2/src/assets/img/logo.svg b/docs/v2/src/assets/img/logo.svg new file mode 100644 index 0000000..9801e58 --- /dev/null +++ b/docs/v2/src/assets/img/logo.svg @@ -0,0 +1,1112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/v2/src/assets/img/spotify-logo.svg b/docs/v2/src/assets/img/spotify-logo.svg new file mode 100644 index 0000000..5202208 --- /dev/null +++ b/docs/v2/src/assets/img/spotify-logo.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/docs/v2/src/assets/img/storybook-logo.svg b/docs/v2/src/assets/img/storybook-logo.svg new file mode 100644 index 0000000..7946497 --- /dev/null +++ b/docs/v2/src/assets/img/storybook-logo.svg @@ -0,0 +1 @@ +logo-storybook-default \ No newline at end of file diff --git a/docs/v2/src/components/Icon.tsx b/docs/v2/src/components/Icon.tsx new file mode 100644 index 0000000..30e8adc --- /dev/null +++ b/docs/v2/src/components/Icon.tsx @@ -0,0 +1,32 @@ +import classnames from 'classnames'; +import { forwardRef } from 'react'; +import type { SVGProps } from 'react'; + +export interface IconProps extends SVGProps { + svg: { src: string; width: number; height: number }; + icon?: string; +} + +export const Icon = forwardRef( + ({ svg, icon, className, ...other }, ref) => { + if (svg) { + const { src, width, height } = svg; + return ( + + {icon ? : } + + ); + } + + return null; + } +); + +export default Icon; diff --git a/docs/v2/src/components/PageContainer.tsx b/docs/v2/src/components/PageContainer.tsx index 0397b8f..a1d66f9 100644 --- a/docs/v2/src/components/PageContainer.tsx +++ b/docs/v2/src/components/PageContainer.tsx @@ -1,9 +1,11 @@ -import { ReactNode } from 'react'; +import classnames from 'classnames'; +import type { ReactNode } from 'react'; export interface PageContainerProps { - children: ReactNode; + children?: ReactNode; + className?: string; } -export const PageContainer = ({ children }: PageContainerProps) => ( -
{children}
+export const PageContainer = ({ children, className }: PageContainerProps) => ( +
{children}
); diff --git a/docs/v2/src/components/index.mdx b/docs/v2/src/components/index.mdx new file mode 100644 index 0000000..abc0310 --- /dev/null +++ b/docs/v2/src/components/index.mdx @@ -0,0 +1,448 @@ + +> OverlayScrollbars is a javascript scrollbar plugin that hides native scrollbars, provides custom styleable overlay scrollbars and keeps the native functionality and feeling. + +## Why + +I created this plugin because I hate ugly and space consuming scrollbars. Similar plugins haven't met my requirements in terms of features, quality, simplicity, license or browser support. + +## Goals & Features + + - Simple, powerful and good documented API + - High browser compatibility - Firefox, Chrome, Opera, Edge, Safari 10+ and IE 11 + - Can be run on the server - SSR, SSG and ISR support + - Tested on various devices - Mobile, Desktop and Tablet + - Tested with various (and mixed) inputs - Mouse, touch and pen + - Treeshaking - bundle only what you really need + - Automatic update detection - no polling + - Usage of latest browser features - best performance in new browsers + - Bidirectional - LTR or RTL direction support + - Simple and effective scrollbar styling + - Highly customizable + - TypeScript support - fully written in TypeScript + - Dependency free - 100% self written to ensure small size and best functionality + +## Getting started + +#### npm & Node +OverlayScrollbars can be downloaded from [npm](https://www.npmjs.com/package/overlayscrollbars) or the package manager of your choice: +```sh +npm install overlayscrollbars +``` +After installation it can be imported: +```js +import 'overlayscrollbars/overlayscrollbars.css'; +import { OverlayScrollbars } from 'overlayscrollbars'; +``` + + +
+ +
Manual download & embedding
+
+ +You can use OverlayScrollbars without any bundler or package manager. +Simply download it from the [Releases](https://github.com/KingSora/OverlayScrollbars/releases) or use a [CDN](https://cdnjs.com/libraries/overlayscrollbars). + +- Use the javascript files with the `.browser` extension. +- If you target old browsers use the `.es5` javascript file, for new browsers `.es6`. +- For production use the javascript / stylesheet files with the `.min` extension. + +Embedd OverlayScrollbars manually in your HTML: +```html + + +``` +
+ + +## Initialization + +> __Note__: During initialization its expected that the CSS file is loaded and parsed by the browser. + +You can initialize either directly with an `Element` or with an `Object` where you have more control over the initialization process. + +```js +// simple initialization with an element +const osInstance = OverlayScrollbars(document.querySelector('#myElement'), {}); +``` + +### Bridging initialization flickering + +If you initialize OverlayScrollbars it needs a few milliseconds to create and append all the elements to the DOM. +While this period the native scrollbars are still visible and are switched out after the initialization is finished. This is perceived as flickering. + +
+ +
Initialization with an Object
+
+ +> __Note__: For now please refer to the TypeScript definitions for a more detailed description of all possibilities. + +The only required field is the `target` field. This is the field to which the plugin is applied to. +If you use the object initialization only with the `target` field, the outcome is equivalent to the element initialization: +```js +// Both initializations have the same outcome + +OverlayScrollbars(document.querySelector('#myElement'), {}); +OverlayScrollbars({ target: document.querySelector('#myElement') }, {}); +``` + +In the initialization object you can specify how the library is handling generated elements. +For example you can appoint an existing element as the `viewport` element. Like this the library won't generate it but take the specified element instead: + + ```js +OverlayScrollbars({ + target: document.querySelector('#target'), + elements: { + viewport: document.querySelector('#viewport'), + }, +}, {}); +``` + +This is very useful if you have a fixed DOM structure and don't want OverlayScrollbars to generate its own elements. Those cases arise very often when you want an other library to work together with OverlayScrollbars. + +--- + +You can also decide to which element the scrollbars should be applied to: + + ```js +OverlayScrollbars({ + target: document.querySelector('#target'), + scrollbars: { + slot: document.querySelector('#target').parentElement, + }, +}, {}); +``` + +--- + +And last but not least you can decide when the initialization should be canceled: + ```js +OverlayScrollbars({ + target: document.querySelector('#target'), + cancel: { + nativeScrollbarsOverlaid: true, + body: null, + } +}, {}); +``` + +In the above example the initialization is canceled when the native scrollbars are overlaid or when your target is a `body` element and the plugin determined that a initialization to the `body` element would affect native functionality like `window.scrollTo`. + +
+ +## Options + +You can initialize OverlayScrollbars with an initial set of options, which can be changed at any time with the `options` method: +```js +OverlayScrollbars(document.querySelector('#myElement'), { + overflow: { + x: 'hidden', + }, +}); +``` + +
+ +
Options in depth
+
+ +The default options are: +```js +const defaultOptions = { + paddingAbsolute: false, + showNativeOverlaidScrollbars: false, + update: { + elementEvents: [['img', 'load']], + debounce: [0, 33], + attributes: null, + ignoreMutation: null, + }, + overflow: { + x: 'scroll', + y: 'scroll', + }, + scrollbars: { + theme: 'os-theme-dark', + visibility: 'auto', + autoHide: 'never', + autoHideDelay: 1300, + dragScroll: true, + clickScroll: false, + pointers: ['mouse', 'touch', 'pen'], + }, +}; +``` + +### `paddingAbsolute` + +| type | default | +| :--- | :--- | +| `boolean` | `false` | + +Indicates whether the padding for the content shall be absolute. + +### `showNativeOverlaidScrollbars` + +| type | default | +| :--- | :--- | +| `boolean` | `false` | + +Indicates whether the native overlaid scrollbars shall be visible. + +### `update.elementEvents` + +| type | default | +| :--- | :--- | +| `Array<[string, string]> \| null` | `[['img', 'load']]` | + +An array of tuples. The first value in the tuple is an `selector` and the second value are `event names`. The plugin will update itself if any of the elements with the specified selector will emit any specified event. The default value can be interpreted as "The plugin will update itself if any `img` element emits an `load` event." + +### `update.debounce` + +| type | default | +| :--- | :--- | +| `[number, number] \| number \| null` | `[0, 33]` | + +> __Note__: If 0 is used for the timeout, `requestAnimationFrame` instead of `setTimeout` is used for the debounce. + +Debounces the `MutationObserver` which tracks changes to the content. If a **tuple** is passed, the first value is the timeout and second is the max wait. If only a **number** is passed you specify only the timeout and there is no max wait. With **null** there is no debounce. **Usefull to fine-tune performance.** + +### `update.attributes` + +| type | default | +| :--- | :--- | +| `string[] \| null` | `null` | + +> __Note__: There is a base array of attributes that the `MutationObserver` always observes, even if this option is `null`. + +An array of additional attributes that the `MutationObserver` should observe for the content. + +### `update.ignoreMutation` + +| type | default | +| :--- | :--- | +| `((mutation) => any) \| null` | `null` | + +A function which receives a [`MutationRecord`](https://developer.mozilla.org/en-US/docs/Web/API/MutationRecord) as an argument. If the function returns a truthy value the mutation will be ignored and the plugin won't update. **Usefull to fine-tune performance.** + +### `overflow.x` + +| type | default | +| :--- | :--- | +| `string` | `'scroll'` | + +> __Note__: Valid values are: `'hidden'`, `'scroll'`, `'visible'`, `'visible-hidden'` and `'visible-scroll'`. + +The overflow behavior for the horizontal (x) axis. + +### `overflow.y` + +| type | default | +| :--- | :--- | +| `string` | `'scroll'` | + +> __Note__: Valid values are: `'hidden'`, `'scroll'`, `'visible'`, `'visible-hidden'` and `'visible-scroll'`. + +The overflow behavior for the vertical (y) axis. + +### `scrollbars.theme` + +| type | default | +| :--- | :--- | +| `string \| null` | `'os-theme-dark'` | + +Applies the specified theme (classname) to the scrollbars. + +### `scrollbars.visibility` + +| type | default | +| :--- | :--- | +| `string` | `'auto'` | + +> __Note__: Valid values are: `'visible'`, `'hidden'`, and `'auto'`. + +The base visibility of the scrollbars. + +### `scrollbars.autoHide` + +| type | default | +| :--- | :--- | +| `string` | `'never'` | + +> __Note__: Valid values are: `'never'`, `'scroll'`, `'leave'` and `'move'`. + +The possibility to hide visible scrollbars automatically after a certain user action. + +### `scrollbars.autoHideDelay` + +| type | default | +| :--- | :--- | +| `number` | `1300` | + +The delay in milliseconds before the scrollbars are hidden automatically. + +### `scrollbars.dragScroll` + +| type | default | +| :--- | :--- | +| `boolean` | `true` | + +Indicates whether you can drag the scrollbar handles for scrolling. + +### `scrollbars.clickScroll` + +| type | default | +| :--- | :--- | +| `boolean` | `false` | + +Indicates whether you can click on the scrollbar track for scrolling. + +### `scrollbars.pointers` + +| type | default | +| :--- | :--- | +| `string[] \| null` | `['mouse', 'touch', 'pen']` | + +The [`PointerTypes`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerType) the plugin should react to. + +
+ +## Events + +You can initialize OverlayScrollbars with an initial set of events, which can be managed at any time with the `on` and `off` methods: +```js +OverlayScrollbars(document.querySelector('#myElement'), {}, { + updated(osInstance, onUpdatedArgs) { + // ... + } +}); +``` + +
+ +
Events in depth
+
+ +> __Note__: Every event receives the `instance` from which it was fired as the first argument. Always. + +### `initialized` + +| arguments | description | +| :--- | :--- | +| `instance` | The instance which fired the event. | + +Is fired after all generated elements, observers and events were appended to the DOM. + +### `updated` + +| arguments | description | +| :--- | :--- | +| `instance` | The instance which fired the event. | +| `onUpdatedArgs` | An `object` which describes the update in detail. | + +> __Note__: If an update was triggered but nothing changed, the event won't be fired. + +Is fired after the instace was updated. + +### `destroyed` + +| arguments | description | +| :--- | :--- | +| `instance` | The instance which fired the event. | +| `canceled` | An `boolean` which indicates whether the initialization was canceled and thus destroyed. | + +Is fired after all generated elements, observers and events were removed from the DOM. + +
+ +## Instance Methods + +> __Note__: For now please refer to the TypeScript definitions for a more detailed description. + +```ts +interface OverlayScrollbars { + options(): Options; + options(newOptions: DeepPartial): Options; + + update(force?: boolean): OverlayScrollbars; + + destroy(): void; + + state(): State; + + elements(): Elements; + + on(name: N, listener: EventListener): () => void; + on(name: N, listener: EventListener[]): () => void; + + off(name: N, listener: EventListener): void; + off(name: N, listener: EventListener[]): void; +} +``` + +## Static Methods + +> __Note__: For now please refer to the TypeScript definitions for a more detailed description. + +```ts +interface OverlayScrollbarsStatic { + (target: InitializationTarget): OverlayScrollbars | undefined; + ( + target: InitializationTarget, + options: DeepPartial, + eventListeners?: InitialEventListeners + ): OverlayScrollbars; + + plugin(plugin: Plugin | Plugin[]): void; + valid(osInstance: any): boolean; + env(): Environment; +} +``` + +## Plugins + +Everything thats considered not core functionality or old browser compatibility is exposed via a plugin. This is done because all unused plugins are treeshaken and thus won't end up in your final bundle. OverlayScrollbars comes with the following plugins: + +- **ScrollbarsHidingPlugin**: Is needed for old browsers which aren't supporting nativ scrollbar styling features. [You can find the list of browsers where you need this plugin here](https://caniuse.com/?search=scrollbar%20styling) (note that even though `iOS Safari >= 14` is marked as unsupported you only need this plugin for `iOS < 7.1`). +- **SizeObserverPlugin**: Is needed for old browsers which aren't supporting the `ResizeObserver` api. [You can find the list of browsers where you need this plugin here](https://caniuse.com/?search=ResizeObserver) +- **ClickScrollPlugin**: If you want to use the option `scrollbars: { clickScroll: true }`. + +#### Consuming Plugins + +You can consume plugins like: +```ts +import { + OverlayScrollbars, + ScrollbarsHidingPlugin, + SizeObserverPlugin, + ClickScrollPlugin +} from 'overlayscrollbars'; + +// single plugin +OverlayScrollbars.plugin(ScrollbarsHidingPlugin); + +// multiple plugins +OverlayScrollbars.plugin([SizeObserverPlugin, ClickScrollPlugin]); +``` + +#### Writing Plugins + +> __Note__: For now please refer to the TypeScript definitions for a more detailed description. + +You can write and publish your own Plugins. This section is a work in progress. + +## Feature comparison to `v1` + +- The `scroll` function is missing. Planned as a `plugin`. (WIP) +- Initialization to the `textarea` element isn't supported yet. Planned as a `plugin`. (WIP) + +## Future Plans + + - Provide plugin based support for missing features. (treeshakeable) + - Frequent updates in terms of bug-fixes and enhancements. (always use latest browser features) + - Improve tests. (unit & browser tests) + +## License + +MIT diff --git a/docs/v2/src/components/md/Heading.tsx b/docs/v2/src/components/md/Heading.tsx new file mode 100644 index 0000000..ce1ecbc --- /dev/null +++ b/docs/v2/src/components/md/Heading.tsx @@ -0,0 +1,21 @@ +// import Link from 'next/link'; +import type { ComponentProps } from 'react'; + +export interface HeadingProps extends ComponentProps<'h1'> { + tag: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'; +} + +export const Heading = ({ id, tag: Tag, ...rest }: HeadingProps) => { + if (id) { + return ( + <> + {/* */} + {/* eslint-disable-next-line */} + + {/* */} + + ); + } + /* eslint-disable-next-line */ + return ; +}; diff --git a/docs/v2/src/components/md/Pre.tsx b/docs/v2/src/components/md/Pre.tsx new file mode 100644 index 0000000..cb8d956 --- /dev/null +++ b/docs/v2/src/components/md/Pre.tsx @@ -0,0 +1,17 @@ +import { useEffect, useRef } from 'react'; +import { OverlayScrollbars } from '@~package/overlayscrollbars'; +import type { ComponentProps } from 'react'; + +export const Pre = ({ children }: ComponentProps<'pre'>) => { + const ref = useRef(null); + useEffect(() => { + if (ref.current) { + const instance = OverlayScrollbars(ref.current, { + paddingAbsolute: true, + scrollbars: { autoHide: 'leave' }, + }); + return () => instance.destroy(); + } + }); + return
{children}
; +}; diff --git a/docs/v2/src/pages/_app.tsx b/docs/v2/src/pages/_app.tsx index 78753ed..6786983 100644 --- a/docs/v2/src/pages/_app.tsx +++ b/docs/v2/src/pages/_app.tsx @@ -1,64 +1,96 @@ import '~/assets/css/tailwind.css'; import '~/assets/css/styles.css'; +import '@~package/overlayscrollbars/overlayscrollbars.css'; +import { useEffect } from 'react'; +import { OverlayScrollbars } from '@~package/overlayscrollbars'; import Head from 'next/head'; +import { MDXProvider } from '@mdx-js/react'; import favicon from '~/assets/favicon.ico'; +import { Pre } from '~/components/md/Pre'; +import { Heading } from '~/components/md/Heading'; +import type { ComponentProps } from 'react'; import type { AppProps } from 'next/app'; +import type { HeadingProps } from '~/components/md/Heading'; -const OverlayScrollbarsDocs = ({ Component, pageProps }: AppProps) => ( -
- - OverlayScrollbars - - - - - - - - - - -