{"id":24408036,"url":"https://github.com/stackblitz-labs/use-stick-to-bottom","last_synced_at":"2025-04-04T11:07:26.759Z","repository":{"id":255931357,"uuid":"853922938","full_name":"stackblitz-labs/use-stick-to-bottom","owner":"stackblitz-labs","description":"A lightweight React Hook intended mainly for AI chat applications, for smoothly sticking to bottom of messages","archived":false,"fork":false,"pushed_at":"2025-01-31T16:35:42.000Z","size":178,"stargazers_count":219,"open_issues_count":3,"forks_count":16,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-28T10:01:57.933Z","etag":null,"topics":["ai","chatbot","chatgpt","claude","messages","smooth-scrolling"],"latest_commit_sha":null,"homepage":"https://stackblitz.com/~/github.com/stackblitz-labs/use-stick-to-bottom?file=demo/Demo.tsx","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stackblitz-labs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-07T23:03:13.000Z","updated_at":"2025-03-27T21:38:44.000Z","dependencies_parsed_at":"2024-09-07T23:27:59.245Z","dependency_job_id":"3df274ee-6689-4913-a4a3-5aa20904d4bb","html_url":"https://github.com/stackblitz-labs/use-stick-to-bottom","commit_stats":{"total_commits":2,"total_committers":1,"mean_commits":2.0,"dds":0.0,"last_synced_commit":"25110a058968e50bcdb64b4ce833dfd0e47c307a"},"previous_names":["samdenty/use-stick-to-bottom","stackblitz/use-stick-to-bottom","stackblitz-labs/use-stick-to-bottom"],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackblitz-labs%2Fuse-stick-to-bottom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackblitz-labs%2Fuse-stick-to-bottom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackblitz-labs%2Fuse-stick-to-bottom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackblitz-labs%2Fuse-stick-to-bottom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stackblitz-labs","download_url":"https://codeload.github.com/stackblitz-labs/use-stick-to-bottom/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247160755,"owners_count":20893894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","chatbot","chatgpt","claude","messages","smooth-scrolling"],"created_at":"2025-01-20T05:18:57.346Z","updated_at":"2025-04-04T11:07:26.730Z","avatar_url":"https://github.com/stackblitz-labs.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# `useStickToBottom`\n\n\u003e Designed with AI chat bots in mind, [powering `bolt.new` by StackBlitz](https://bolt.new)\n\n[![npm version](https://img.shields.io/npm/v/use-stick-to-bottom.svg?style=flat-square)](https://www.npmjs.com/package/use-stick-to-bottom)\n[![npm downloads](https://img.shields.io/npm/dm/use-stick-to-bottom.svg?style=flat-square)](https://www.npmjs.com/package/use-stick-to-bottom)\n[![Demo](https://img.shields.io/badge/StackBlitz-Demo-blue.svg?style=flat-square)](https://stackblitz.com/~/github.com/samdenty/use-stick-to-bottom?file=demo/Demo.tsx)\n\nA lightweight **zero-dependency** React hook + Component that automatically sticks to the bottom of container and smoothly animates the content to keep it's visual position on screen whilst new content is being added.\n\n## Features\n\n- Does not require [`overflow-anchor`](https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-anchor) browser-level CSS support which Safari does not support.\n- Can be connected up to any existing component using a hook with refs. Or simply use the provided component, which handles the refs for you plus provides context - so child components can check `isAtBottom` \u0026 programmatically scroll to the bottom.\n- Uses the modern, yet well-supported, [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) API to detect when content resizes.\n  - Supports content shrinking without losing stickiness - not just getting taller.\n- Correctly handles [Scroll Anchoring](https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-anchor/Guide_to_scroll_anchoring). This is where when content above the viewport resizes, it doesn't cause the content currently displayed in viewport to jump up or down.\n- Allows the user to cancel the stickiness at any time by scrolling up.\n  - Clever logic distinguishes the user scrolling from the custom animation scroll events (without doing any debouncing which could cause some events to be missed).\n  - Mobile devices work well with this logic too.\n- Uses a custom implemented smooth scrolling algorithm, featuring velocity-based spring animations (with configurable parameters).\n  - Other libraries use easing functions with durations instead, but these doesn't work well when you want to stream in new content with variable sizing - which is common for AI chatbot use cases.\n  - `scrollToBottom` returns a `Promise\u003cboolean\u003e` which will resolve to `true` as soon as the scroll was successful, or `false` if the scroll was cancelled.\n\n# Usage\n\n## `\u003cStickToBottom\u003e` Component\n\n```jsx\nimport { StickToBottom, useStickToBottomContext } from 'use-stick-to-bottom';\n\nfunction Chat() {\n  return (\n    \u003cStickToBottom className=\"h-[50vh] relative\" resize=\"smooth\" initial=\"smooth\"\u003e\n      \u003cStickToBottom.Content className=\"flex flex-col gap-4\"\u003e\n        {messages.map((message) =\u003e (\n          \u003cMessage key={message.id} message={message} /\u003e\n        ))}\n      \u003c/StickToBottom.Content\u003e\n\n      \u003cScrollToBottom /\u003e\n\n      {/* This component uses `useStickToBottomContext` to scroll to bottom when the user enters a message */}\n      \u003cChatBox /\u003e\n    \u003c/StickToBottom\u003e\n  );\n}\n\nfunction ScrollToBottom() {\n  const { isAtBottom, scrollToBottom } = useStickToBottomContext();\n\n  return (\n    !isAtBottom \u0026\u0026 (\n      \u003cbutton\n        className=\"absolute i-ph-arrow-circle-down-fill text-4xl rounded-lg left-[50%] translate-x-[-50%] bottom-0\"\n        onClick={() =\u003e scrollToBottom()}\n      /\u003e\n    )\n  );\n}\n```\n\n## `useStickToBottom` Hook\n\n```jsx\nimport { useStickToBottom } from 'use-stick-to-bottom';\n\nfunction Component() {\n  const { scrollRef, contentRef } = useStickToBottom();\n\n  return (\n    \u003cdiv style={{ overflow: 'auto' }} ref={scrollRef}\u003e\n      \u003cdiv ref={contentRef}\u003e\n        {messages.map((message) =\u003e (\n          \u003cMessage key={message.id} message={message} /\u003e\n        ))}\n      \u003c/div\u003e\n    \u003c/div\u003e\n  );\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstackblitz-labs%2Fuse-stick-to-bottom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstackblitz-labs%2Fuse-stick-to-bottom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstackblitz-labs%2Fuse-stick-to-bottom/lists"}