Simulate Chatting
- Simulate Chatting based on swiper 4+. (Demo)
Since v3 the package is ESM-only, ships TypeScript declarations and is built with Vite. See Migrating from v2 to v3.
Install
# via npm
$ npm install @cycjimmy/simulate-chatting --save
# or via yarn
$ yarn add @cycjimmy/simulate-chatting
Use
Simulate chatting based on Swiper 4+. Swiper is not bundled: load it yourself and either pass it in through SwiperModule or expose it as the global Swiper.
import Swiper from 'swiper/bundle'
import 'swiper/css/bundle'
import SimulateChat from '@cycjimmy/simulate-chatting'
const simulateChat = new SimulateChat(wrapper, {
sound: 'msg.mp3',
chartList: [],
SwiperModule: Swiper,
})
const simulateChat = new SimulateChat(wrapper, options)
wrapper: [Element|String] Context Wrapper Element. [Required]options: looks like: { sound: 'source', chartList: [] }footer: [Object]. Defaultnull.height: [Number|String] Height of footer input. If you want to add the unit, can use the string form. E.g:'20vw'.img: [String] Image source.
sound: [String] Audio source. Default''.chartList: [Array] Array of chartList patterns. Default[]. The array and its items are not modified.- A pattern looks like:
{ pos: 'left', w: 100, h: 30, img: '', delay: 2000 [, ...] }pos: [String] Position of the pattern. Use one of the three options:'left'(default when omitted)'center''right'
w: [Number|String] Width of pattern. If you want to add the unit, can use the string form. E.g:'20vw'.h: [Number|String] Height of pattern. If you want to add the unit, can use the string form. E.g:'20vw'.top: [Number|String] Custom set the margin from the previous. If you want to add the unit, can use the string form. E.g:'20vw'.img: [String] Image source.delay: [Number] Delay from the previous pattern display. Default1500.custom: [Boolean] Whether to enable custom mode.html: [String] Custom html structure of pattern when custom istrue.pause: [Boolean] Whether to pause the running of patterns after this pattern display.muted: [Boolean] Whether not to play the sound when this pattern show.callback: [Function] if set, run callback function after this pattern show.
- A pattern looks like:
SwiperModule: [Function] The Swiper constructor to use. Note the version to 4+. Defaults to the globalwindow.Swiper.
Functions:
start(): Start to display patterns. Returns the instance while it is busy or already finished, and a promise otherwise.pause(): Pause the running of patterns.reset(): Reset patterns.
The package injects its own stylesheet when it is imported, so no extra CSS file is published. Swiper's stylesheet still has to be loaded by the application.
Use in browser
<div id="wrapper"></div>
<script src="swiper-bundle.min.js"></script>
<script src="simulate-chatting.umd.min.js"></script>
<script>
const simulateChat = new SimulateChat('#wrapper', {
sound: 'msg.mp3',
footer: {
height: 40,
img: 'footerInput.jpg'
},
chartList: [...]
});
simulateChat.start();
</script>
The UMD build exposes the constructor as window.SimulateChat.
TypeScript
The published entry point carries its own declarations, so no @types package
is needed:
import type { ChartItem, SimulateChatOptions } from '@cycjimmy/simulate-chatting'
SwiperModule is declared structurally and never references the swiper
package's types, so the declarations resolve even when Swiper is loaded from a
script tag.
CDN
To use via a CDN include this in your HTML:
<script src="https://cdn.jsdelivr.net/npm/@cycjimmy/simulate-chatting@3/dist/simulate-chatting.umd.min.js"></script>
Migrating from v2 to v3
v3 is a breaking release. In order of likelihood to affect an existing application:
- CommonJS output is gone.
main,moduleandexportsall resolve todist/simulate-chatting.esm.js, and therequirecondition plus thedist/simulate-chatting.cjsbundle were removed. On Node ≥ 22.12 arequire()still loads the ESM entry, but it returns the module namespace: useimport SimulateChat from '@cycjimmy/simulate-chatting', or read.defaultfrom a dynamic import. - TypeScript declarations are published from
dist/index.d.ts. - Old browsers are no longer targeted. The output is ES2015 and the CSS is
generated for the
defaultsbrowserslist; the previous IE10 targets (and their prefixes) are gone. - The
chartListyou pass in is not mutated any more. The resolved index and the pixel-convertedw/h/topvalues live on an internal copy instead of being written back onto your objects. - An omitted
delaynow waits the documented 1500 ms. It used to be written to the markup as the stringundefined, which the browser turned into a ~0 ms timeout. start()without asoundoption no longer throws. Every audio call is skipped when no source is configured.- The toolchain requires a newer Node. Building, testing and releasing need
Node
^22.22.2 || ^24.15.0 || >=26.0.0; the published bundle has no Node dependency and carries noenginesfield. destroy()is still not provided: to stop a running instance, stop calling into it and remove the wrapper element.
Development
$ npm install
$ npm run dev # Vite dev server, opens index.html
$ npm run type-check # tsc --noEmit
$ npm run lint # ESLint (flat config, @antfu/eslint-config)
$ npm run lint:style # Stylelint for src/**/*.{css,scss}
$ npm test # Vitest
$ npm run package # build dist/ and assemble .release/
See CONTRIBUTING.md for the full workflow.