@hapticjs/core 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2622,6 +2622,734 @@ var physics = {
2622
2622
  pendulum
2623
2623
  };
2624
2624
 
2625
+ // src/middleware/middleware.ts
2626
+ var MiddlewareManager = class {
2627
+ constructor() {
2628
+ this.middleware = [];
2629
+ }
2630
+ /** Register a middleware */
2631
+ use(middleware) {
2632
+ this.middleware.push(middleware);
2633
+ }
2634
+ /** Remove a middleware by name */
2635
+ remove(name) {
2636
+ this.middleware = this.middleware.filter((m) => m.name !== name);
2637
+ }
2638
+ /** Run all middleware in order */
2639
+ process(steps) {
2640
+ let result = steps;
2641
+ for (const m of this.middleware) {
2642
+ result = m.process(result);
2643
+ }
2644
+ return result;
2645
+ }
2646
+ /** Remove all middleware */
2647
+ clear() {
2648
+ this.middleware = [];
2649
+ }
2650
+ /** List registered middleware names */
2651
+ list() {
2652
+ return this.middleware.map((m) => m.name);
2653
+ }
2654
+ };
2655
+ function intensityScaler(scale) {
2656
+ return {
2657
+ name: "intensityScaler",
2658
+ process: (steps) => steps.map((s) => ({
2659
+ ...s,
2660
+ intensity: Math.min(1, Math.max(0, s.intensity * scale))
2661
+ }))
2662
+ };
2663
+ }
2664
+ function durationScaler(scale) {
2665
+ return {
2666
+ name: "durationScaler",
2667
+ process: (steps) => steps.map((s) => ({
2668
+ ...s,
2669
+ duration: Math.max(20, Math.round(s.duration * scale))
2670
+ }))
2671
+ };
2672
+ }
2673
+ function intensityClamper(min, max) {
2674
+ return {
2675
+ name: "intensityClamper",
2676
+ process: (steps) => steps.map((s) => ({
2677
+ ...s,
2678
+ intensity: Math.min(max, Math.max(min, s.intensity))
2679
+ }))
2680
+ };
2681
+ }
2682
+ function patternRepeater(times) {
2683
+ return {
2684
+ name: "patternRepeater",
2685
+ process: (steps) => {
2686
+ const result = [];
2687
+ for (let i = 0; i < times; i++) {
2688
+ result.push(...steps.map((s) => ({ ...s })));
2689
+ }
2690
+ return result;
2691
+ }
2692
+ };
2693
+ }
2694
+ function reverser() {
2695
+ return {
2696
+ name: "reverser",
2697
+ process: (steps) => [...steps].reverse()
2698
+ };
2699
+ }
2700
+ function accessibilityBooster() {
2701
+ return {
2702
+ name: "accessibilityBooster",
2703
+ process: (steps) => steps.map((s) => ({
2704
+ ...s,
2705
+ intensity: Math.min(1, s.intensity * 1.3),
2706
+ duration: Math.max(20, Math.round(s.duration * 1.2))
2707
+ }))
2708
+ };
2709
+ }
2710
+
2711
+ // src/profiles/intensity-profiles.ts
2712
+ var profiles = {
2713
+ off: {
2714
+ name: "off",
2715
+ hapticScale: 0,
2716
+ durationScale: 0,
2717
+ soundEnabled: false,
2718
+ soundVolume: 0,
2719
+ visualEnabled: false
2720
+ },
2721
+ subtle: {
2722
+ name: "subtle",
2723
+ hapticScale: 0.5,
2724
+ durationScale: 0.7,
2725
+ soundEnabled: true,
2726
+ soundVolume: 0.1,
2727
+ visualEnabled: true
2728
+ },
2729
+ normal: {
2730
+ name: "normal",
2731
+ hapticScale: 1,
2732
+ durationScale: 1,
2733
+ soundEnabled: true,
2734
+ soundVolume: 0.3,
2735
+ visualEnabled: true
2736
+ },
2737
+ strong: {
2738
+ name: "strong",
2739
+ hapticScale: 1.3,
2740
+ durationScale: 1.2,
2741
+ soundEnabled: true,
2742
+ soundVolume: 0.5,
2743
+ visualEnabled: true
2744
+ },
2745
+ intense: {
2746
+ name: "intense",
2747
+ hapticScale: 1.8,
2748
+ durationScale: 1.5,
2749
+ soundEnabled: true,
2750
+ soundVolume: 0.7,
2751
+ visualEnabled: true
2752
+ },
2753
+ accessible: {
2754
+ name: "accessible",
2755
+ hapticScale: 1.5,
2756
+ durationScale: 1.3,
2757
+ soundEnabled: true,
2758
+ soundVolume: 0.6,
2759
+ visualEnabled: true
2760
+ }
2761
+ };
2762
+ var ProfileManager = class {
2763
+ constructor() {
2764
+ this.registry = /* @__PURE__ */ new Map();
2765
+ for (const [name, profile] of Object.entries(profiles)) {
2766
+ this.registry.set(name, profile);
2767
+ }
2768
+ this.currentProfile = profiles.normal;
2769
+ }
2770
+ /** Apply a profile by name or custom profile object */
2771
+ setProfile(name) {
2772
+ if (typeof name === "string") {
2773
+ const profile = this.registry.get(name);
2774
+ if (!profile) {
2775
+ throw new Error(`Unknown profile: "${name}"`);
2776
+ }
2777
+ this.currentProfile = profile;
2778
+ } else {
2779
+ this.registry.set(name.name, name);
2780
+ this.currentProfile = name;
2781
+ }
2782
+ }
2783
+ /** Get the current profile */
2784
+ getProfile() {
2785
+ return this.currentProfile;
2786
+ }
2787
+ /** List available profile names */
2788
+ listProfiles() {
2789
+ return Array.from(this.registry.keys());
2790
+ }
2791
+ /** Register a custom profile */
2792
+ registerProfile(profile) {
2793
+ this.registry.set(profile.name, profile);
2794
+ }
2795
+ /** Convert current profile to a HapticMiddleware (intensity + duration scaling) */
2796
+ toMiddleware() {
2797
+ const profile = this.currentProfile;
2798
+ const iScaler = intensityScaler(profile.hapticScale);
2799
+ const dScaler = durationScaler(profile.durationScale);
2800
+ return {
2801
+ name: `profile:${profile.name}`,
2802
+ process: (steps) => dScaler.process(iScaler.process(steps))
2803
+ };
2804
+ }
2805
+ /** Current profile name */
2806
+ get current() {
2807
+ return this.currentProfile.name;
2808
+ }
2809
+ };
2810
+
2811
+ // src/experiment/ab-testing.ts
2812
+ var HapticExperiment = class {
2813
+ constructor(name, variants) {
2814
+ this.assignments = /* @__PURE__ */ new Map();
2815
+ this.tracking = [];
2816
+ this._name = name;
2817
+ this.variants = variants;
2818
+ this.variantNames = Object.keys(variants);
2819
+ if (this.variantNames.length === 0) {
2820
+ throw new Error("Experiment must have at least one variant");
2821
+ }
2822
+ }
2823
+ /** Experiment name */
2824
+ get name() {
2825
+ return this._name;
2826
+ }
2827
+ /**
2828
+ * Randomly assign a variant (consistent for same userId via simple hash).
2829
+ * If no userId is provided, generates a random assignment.
2830
+ */
2831
+ assign(userId) {
2832
+ const id = userId ?? `anon-${Math.random().toString(36).slice(2)}`;
2833
+ const existing = this.assignments.get(id);
2834
+ if (existing) return existing;
2835
+ const hash = this._hash(id);
2836
+ const index = hash % this.variantNames.length;
2837
+ const variant = this.variantNames[index];
2838
+ this.assignments.set(id, variant);
2839
+ return variant;
2840
+ }
2841
+ /** Get the assigned variant pattern for a user */
2842
+ getVariant(userId) {
2843
+ const id = userId ?? void 0;
2844
+ if (!id) return void 0;
2845
+ const variant = this.assignments.get(id);
2846
+ if (!variant) return void 0;
2847
+ return this.variants[variant];
2848
+ }
2849
+ /** Track an event for a user */
2850
+ track(userId, event, value) {
2851
+ const variant = this.assignments.get(userId);
2852
+ if (!variant) return;
2853
+ this.tracking.push({
2854
+ userId,
2855
+ variant,
2856
+ event,
2857
+ value,
2858
+ timestamp: Date.now()
2859
+ });
2860
+ }
2861
+ /** Get aggregated results per variant */
2862
+ getResults() {
2863
+ const results = {};
2864
+ for (const name of this.variantNames) {
2865
+ results[name] = { assignments: 0, events: {} };
2866
+ }
2867
+ for (const variant of this.assignments.values()) {
2868
+ if (results[variant]) {
2869
+ results[variant].assignments++;
2870
+ }
2871
+ }
2872
+ for (const entry of this.tracking) {
2873
+ if (results[entry.variant]) {
2874
+ const events = results[entry.variant].events;
2875
+ events[entry.event] = (events[entry.event] ?? 0) + 1;
2876
+ }
2877
+ }
2878
+ return results;
2879
+ }
2880
+ /** Clear all tracking data and assignments */
2881
+ reset() {
2882
+ this.assignments.clear();
2883
+ this.tracking = [];
2884
+ }
2885
+ /** Simple string hash for deterministic variant assignment */
2886
+ _hash(str) {
2887
+ let hash = 0;
2888
+ for (let i = 0; i < str.length; i++) {
2889
+ const char = str.charCodeAt(i);
2890
+ hash = (hash << 5) - hash + char;
2891
+ hash = hash & hash;
2892
+ }
2893
+ return Math.abs(hash);
2894
+ }
2895
+ };
2896
+
2897
+ // src/rhythm/rhythm-sync.ts
2898
+ var RhythmSync = class {
2899
+ constructor(options = {}) {
2900
+ // reserved for pattern-based rhythm
2901
+ this._isPlaying = false;
2902
+ this._beatCount = 0;
2903
+ this._intervalId = null;
2904
+ this._callbacks = [];
2905
+ this._tapTimestamps = [];
2906
+ this._audioElement = null;
2907
+ this._syncEngine = null;
2908
+ this._syncEffect = "tap";
2909
+ this._bpm = Math.min(300, Math.max(60, options.bpm ?? 120));
2910
+ this._intensity = Math.min(1, Math.max(0, options.intensity ?? 0.7));
2911
+ this._pattern = options.pattern ?? "";
2912
+ }
2913
+ // ─── BPM Control ─────────────────────────────────────────
2914
+ /** Set beats per minute (clamped to 60-300) */
2915
+ setBPM(bpm) {
2916
+ this._bpm = Math.min(300, Math.max(60, bpm));
2917
+ if (this._isPlaying) {
2918
+ this._stopInterval();
2919
+ this._startInterval();
2920
+ }
2921
+ }
2922
+ /**
2923
+ * Store an audio element reference for sync.
2924
+ * Since Web Audio API's AnalyserNode isn't reliably available in all
2925
+ * environments, use tapTempo() for BPM detection instead.
2926
+ */
2927
+ detectBPM(audioElement) {
2928
+ this._audioElement = audioElement;
2929
+ }
2930
+ /**
2931
+ * Tap tempo — call repeatedly to set BPM from tap intervals.
2932
+ * Calculates average BPM from the last 4-8 taps.
2933
+ * Returns the current estimated BPM.
2934
+ */
2935
+ tapTempo() {
2936
+ const now = Date.now();
2937
+ this._tapTimestamps.push(now);
2938
+ if (this._tapTimestamps.length > 8) {
2939
+ this._tapTimestamps = this._tapTimestamps.slice(-8);
2940
+ }
2941
+ if (this._tapTimestamps.length < 2) {
2942
+ return this._bpm;
2943
+ }
2944
+ const timestamps = this._tapTimestamps.slice(-8);
2945
+ const intervals = [];
2946
+ for (let i = 1; i < timestamps.length; i++) {
2947
+ intervals.push(timestamps[i] - timestamps[i - 1]);
2948
+ }
2949
+ const avgInterval = intervals.reduce((sum, v) => sum + v, 0) / intervals.length;
2950
+ if (avgInterval > 0) {
2951
+ const estimatedBPM = Math.round(6e4 / avgInterval);
2952
+ this._bpm = Math.min(300, Math.max(60, estimatedBPM));
2953
+ }
2954
+ return this._bpm;
2955
+ }
2956
+ // ─── Playback ────────────────────────────────────────────
2957
+ /** Start emitting beats at the current BPM */
2958
+ start(callback) {
2959
+ if (this._isPlaying) return;
2960
+ if (callback) {
2961
+ this._callbacks.push(callback);
2962
+ }
2963
+ this._isPlaying = true;
2964
+ this._beatCount = 0;
2965
+ this._startInterval();
2966
+ }
2967
+ /** Stop the rhythm */
2968
+ stop() {
2969
+ this._isPlaying = false;
2970
+ this._stopInterval();
2971
+ }
2972
+ /** Register a beat callback */
2973
+ onBeat(callback) {
2974
+ this._callbacks.push(callback);
2975
+ }
2976
+ // ─── Haptic Sync ────────────────────────────────────────
2977
+ /**
2978
+ * Auto-trigger a haptic effect on each beat.
2979
+ * Calls engine.tap() by default, or the specified semantic method.
2980
+ */
2981
+ syncHaptic(engine, effect = "tap") {
2982
+ this._syncEngine = engine;
2983
+ this._syncEffect = effect;
2984
+ }
2985
+ // ─── Getters ─────────────────────────────────────────────
2986
+ /** Current BPM */
2987
+ get bpm() {
2988
+ return this._bpm;
2989
+ }
2990
+ /** Whether rhythm is active */
2991
+ get isPlaying() {
2992
+ return this._isPlaying;
2993
+ }
2994
+ /** Total beats since start */
2995
+ get beatCount() {
2996
+ return this._beatCount;
2997
+ }
2998
+ /** Current pattern string */
2999
+ get pattern() {
3000
+ return this._pattern;
3001
+ }
3002
+ /** The attached audio element, if any */
3003
+ get audioElement() {
3004
+ return this._audioElement;
3005
+ }
3006
+ // ─── Lifecycle ───────────────────────────────────────────
3007
+ /** Clean up intervals and callbacks */
3008
+ dispose() {
3009
+ this.stop();
3010
+ this._callbacks = [];
3011
+ this._tapTimestamps = [];
3012
+ this._syncEngine = null;
3013
+ this._audioElement = null;
3014
+ }
3015
+ // ─── Internal ────────────────────────────────────────────
3016
+ _startInterval() {
3017
+ const intervalMs = 6e4 / this._bpm;
3018
+ this._intervalId = setInterval(() => {
3019
+ this._beatCount++;
3020
+ const beat = this._beatCount;
3021
+ for (const cb of this._callbacks) {
3022
+ cb(beat);
3023
+ }
3024
+ if (this._syncEngine) {
3025
+ const engine = this._syncEngine;
3026
+ if (typeof engine[this._syncEffect] === "function") {
3027
+ engine[this._syncEffect](this._intensity);
3028
+ } else if (typeof engine.tap === "function") {
3029
+ engine.tap(this._intensity);
3030
+ }
3031
+ }
3032
+ }, intervalMs);
3033
+ }
3034
+ _stopInterval() {
3035
+ if (this._intervalId !== null) {
3036
+ clearInterval(this._intervalId);
3037
+ this._intervalId = null;
3038
+ }
3039
+ }
3040
+ };
3041
+
3042
+ // src/motion/motion-detector.ts
3043
+ var MotionDetector = class {
3044
+ constructor(options = {}) {
3045
+ this._isListening = false;
3046
+ this._callbacks = {
3047
+ shake: [],
3048
+ tilt: [],
3049
+ rotation: [],
3050
+ flip: []
3051
+ };
3052
+ this._lastOrientation = null;
3053
+ this._lastFlipState = null;
3054
+ this._boundMotionHandler = null;
3055
+ this._boundOrientationHandler = null;
3056
+ this._shakeThreshold = options.shakeThreshold ?? 15;
3057
+ this._tiltThreshold = options.tiltThreshold ?? 10;
3058
+ }
3059
+ // ─── Detection ───────────────────────────────────────────
3060
+ /** Whether DeviceMotion API is available */
3061
+ get isSupported() {
3062
+ if (typeof window === "undefined") return false;
3063
+ return "DeviceMotionEvent" in window;
3064
+ }
3065
+ /** Whether currently listening for motion events */
3066
+ get isListening() {
3067
+ return this._isListening;
3068
+ }
3069
+ // ─── Permission ──────────────────────────────────────────
3070
+ /**
3071
+ * Request permission for motion events (required on iOS 13+).
3072
+ * Returns true if permission was granted.
3073
+ */
3074
+ async requestPermission() {
3075
+ if (typeof window === "undefined") return false;
3076
+ const DeviceMotionEventRef = window.DeviceMotionEvent;
3077
+ if (DeviceMotionEventRef && typeof DeviceMotionEventRef.requestPermission === "function") {
3078
+ try {
3079
+ const result = await DeviceMotionEventRef.requestPermission();
3080
+ return result === "granted";
3081
+ } catch {
3082
+ return false;
3083
+ }
3084
+ }
3085
+ return this.isSupported;
3086
+ }
3087
+ // ─── Lifecycle ───────────────────────────────────────────
3088
+ /** Begin listening to device motion and orientation events */
3089
+ start() {
3090
+ if (this._isListening) return;
3091
+ if (typeof window === "undefined") return;
3092
+ this._boundMotionHandler = this._handleMotion.bind(this);
3093
+ this._boundOrientationHandler = this._handleOrientation.bind(this);
3094
+ window.addEventListener("devicemotion", this._boundMotionHandler);
3095
+ window.addEventListener("deviceorientation", this._boundOrientationHandler);
3096
+ this._isListening = true;
3097
+ }
3098
+ /** Stop listening to motion events */
3099
+ stop() {
3100
+ if (!this._isListening) return;
3101
+ if (typeof window === "undefined") return;
3102
+ if (this._boundMotionHandler) {
3103
+ window.removeEventListener("devicemotion", this._boundMotionHandler);
3104
+ }
3105
+ if (this._boundOrientationHandler) {
3106
+ window.removeEventListener("deviceorientation", this._boundOrientationHandler);
3107
+ }
3108
+ this._isListening = false;
3109
+ this._lastOrientation = null;
3110
+ this._lastFlipState = null;
3111
+ }
3112
+ // ─── Callbacks ───────────────────────────────────────────
3113
+ /** Register callback for shake events. Intensity is 0-1 based on acceleration. */
3114
+ onShake(callback) {
3115
+ this._callbacks.shake.push(callback);
3116
+ }
3117
+ /** Register callback for tilt changes. Direction x/y are -1 to 1. */
3118
+ onTilt(callback) {
3119
+ this._callbacks.tilt.push(callback);
3120
+ }
3121
+ /** Register callback for rotation. Angle in degrees. */
3122
+ onRotation(callback) {
3123
+ this._callbacks.rotation.push(callback);
3124
+ }
3125
+ /** Register callback for device flip (face-down/up toggle). */
3126
+ onFlip(callback) {
3127
+ this._callbacks.flip.push(callback);
3128
+ }
3129
+ // ─── Cleanup ─────────────────────────────────────────────
3130
+ /** Remove all listeners and callbacks */
3131
+ dispose() {
3132
+ this.stop();
3133
+ this._callbacks = { shake: [], tilt: [], rotation: [], flip: [] };
3134
+ }
3135
+ // ─── Internal ────────────────────────────────────────────
3136
+ _handleMotion(event) {
3137
+ const accel = event.accelerationIncludingGravity;
3138
+ if (!accel) return;
3139
+ const { x, y, z } = accel;
3140
+ if (x == null || y == null || z == null) return;
3141
+ const magnitude = Math.sqrt(x * x + y * y + z * z) - 9.81;
3142
+ if (magnitude > this._shakeThreshold) {
3143
+ const intensity = Math.min(
3144
+ 1,
3145
+ (magnitude - this._shakeThreshold) / this._shakeThreshold
3146
+ );
3147
+ for (const cb of this._callbacks.shake) {
3148
+ cb(intensity);
3149
+ }
3150
+ }
3151
+ }
3152
+ _handleOrientation(event) {
3153
+ const { alpha, beta, gamma } = event;
3154
+ if (beta == null || gamma == null) return;
3155
+ if (this._lastOrientation) {
3156
+ const deltaBeta = Math.abs(beta - this._lastOrientation.beta);
3157
+ const deltaGamma = Math.abs(gamma - this._lastOrientation.gamma);
3158
+ if (deltaBeta > this._tiltThreshold || deltaGamma > this._tiltThreshold) {
3159
+ const tiltX = Math.max(-1, Math.min(1, gamma / 90));
3160
+ const tiltY = Math.max(-1, Math.min(1, beta / 180));
3161
+ for (const cb of this._callbacks.tilt) {
3162
+ cb({ x: tiltX, y: tiltY });
3163
+ }
3164
+ }
3165
+ }
3166
+ this._lastOrientation = { beta, gamma };
3167
+ if (alpha != null) {
3168
+ for (const cb of this._callbacks.rotation) {
3169
+ cb(alpha);
3170
+ }
3171
+ }
3172
+ const isFaceDown = Math.abs(beta) > 140;
3173
+ if (this._lastFlipState !== null && isFaceDown !== this._lastFlipState) {
3174
+ for (const cb of this._callbacks.flip) {
3175
+ cb();
3176
+ }
3177
+ }
3178
+ this._lastFlipState = isFaceDown;
3179
+ }
3180
+ };
3181
+
3182
+ // src/accessibility/haptic-a11y.ts
3183
+ var HapticA11y = class {
3184
+ constructor(engine, options = {}) {
3185
+ this._isAttached = false;
3186
+ this._root = null;
3187
+ this._observer = null;
3188
+ this._focusChangeCallback = null;
3189
+ this._formErrorCallback = null;
3190
+ // Bound handlers for cleanup
3191
+ this._handleFocusIn = null;
3192
+ this._handleFocusOut = null;
3193
+ this._handleInvalid = null;
3194
+ this._handleClick = null;
3195
+ this.engine = engine;
3196
+ this.options = {
3197
+ focusChange: options.focusChange ?? true,
3198
+ formErrors: options.formErrors ?? true,
3199
+ navigation: options.navigation ?? true,
3200
+ announcements: options.announcements ?? true
3201
+ };
3202
+ }
3203
+ /** Whether currently attached and listening */
3204
+ get isAttached() {
3205
+ return this._isAttached;
3206
+ }
3207
+ // ─── Attach / Detach ─────────────────────────────────────
3208
+ /**
3209
+ * Attach to a root element and begin listening for interactions.
3210
+ * Defaults to document.body if no root is provided.
3211
+ */
3212
+ attach(root) {
3213
+ if (this._isAttached) return;
3214
+ if (typeof document === "undefined") return;
3215
+ this._root = root ?? document.body;
3216
+ if (!this._root) return;
3217
+ this._bindHandlers();
3218
+ this._attachListeners();
3219
+ this._startObserver();
3220
+ this._isAttached = true;
3221
+ }
3222
+ /** Remove all listeners and stop observing */
3223
+ detach() {
3224
+ if (!this._isAttached) return;
3225
+ this._removeListeners();
3226
+ this._stopObserver();
3227
+ this._isAttached = false;
3228
+ this._root = null;
3229
+ }
3230
+ // ─── Custom Handlers ─────────────────────────────────────
3231
+ /** Set a custom handler for focus changes */
3232
+ onFocusChange(callback) {
3233
+ this._focusChangeCallback = callback ?? null;
3234
+ }
3235
+ /** Set a custom handler for form errors */
3236
+ onFormError(callback) {
3237
+ this._formErrorCallback = callback ?? null;
3238
+ }
3239
+ // ─── Cleanup ─────────────────────────────────────────────
3240
+ /** Clean up all listeners, observers, and callbacks */
3241
+ dispose() {
3242
+ this.detach();
3243
+ this._focusChangeCallback = null;
3244
+ this._formErrorCallback = null;
3245
+ }
3246
+ // ─── Internal ────────────────────────────────────────────
3247
+ _bindHandlers() {
3248
+ this._handleFocusIn = (e) => {
3249
+ if (!this.options.focusChange) return;
3250
+ if (this._focusChangeCallback) {
3251
+ this._focusChangeCallback(e);
3252
+ return;
3253
+ }
3254
+ this._safeCall(() => this.engine.selection());
3255
+ };
3256
+ this._handleFocusOut = (_e) => {
3257
+ };
3258
+ this._handleInvalid = (e) => {
3259
+ if (!this.options.formErrors) return;
3260
+ if (this._formErrorCallback) {
3261
+ this._formErrorCallback(e);
3262
+ return;
3263
+ }
3264
+ this._safeCall(() => this.engine.error());
3265
+ };
3266
+ this._handleClick = (e) => {
3267
+ const target = e.target;
3268
+ if (!target) return;
3269
+ const tagName = target.tagName?.toLowerCase();
3270
+ if (tagName === "button" || target.getAttribute?.("role") === "button") {
3271
+ this._safeCall(() => this.engine.tap());
3272
+ return;
3273
+ }
3274
+ if (tagName === "a" && this.options.navigation) {
3275
+ this._safeCall(() => this.engine.selection());
3276
+ return;
3277
+ }
3278
+ if (tagName === "input") {
3279
+ const inputType = target.type?.toLowerCase();
3280
+ if (inputType === "checkbox" || inputType === "radio") {
3281
+ const checked = target.checked;
3282
+ this._safeCall(() => this.engine.toggle(checked));
3283
+ }
3284
+ }
3285
+ };
3286
+ }
3287
+ _attachListeners() {
3288
+ if (!this._root) return;
3289
+ this._root.addEventListener("focusin", this._handleFocusIn, true);
3290
+ this._root.addEventListener("focusout", this._handleFocusOut, true);
3291
+ this._root.addEventListener("invalid", this._handleInvalid, true);
3292
+ this._root.addEventListener("click", this._handleClick, true);
3293
+ }
3294
+ _removeListeners() {
3295
+ if (!this._root) return;
3296
+ this._root.removeEventListener("focusin", this._handleFocusIn, true);
3297
+ this._root.removeEventListener("focusout", this._handleFocusOut, true);
3298
+ this._root.removeEventListener("invalid", this._handleInvalid, true);
3299
+ this._root.removeEventListener("click", this._handleClick, true);
3300
+ }
3301
+ _startObserver() {
3302
+ if (typeof MutationObserver === "undefined") return;
3303
+ if (!this.options.announcements) return;
3304
+ this._observer = new MutationObserver((mutations) => {
3305
+ for (const mutation of mutations) {
3306
+ for (const node of mutation.addedNodes) {
3307
+ if (node.nodeType !== 1) continue;
3308
+ const el = node;
3309
+ const role = el.getAttribute?.("role");
3310
+ const ariaLive = el.getAttribute?.("aria-live");
3311
+ if (role === "alert" || role === "alertdialog") {
3312
+ this._safeCall(() => this.engine.warning());
3313
+ } else if (role === "status" || ariaLive === "polite" || ariaLive === "assertive") {
3314
+ this._safeCall(() => this.engine.selection());
3315
+ }
3316
+ if (role === "dialog" || el.tagName?.toLowerCase() === "dialog") {
3317
+ this._safeCall(() => this.engine.toggle(true));
3318
+ }
3319
+ }
3320
+ for (const node of mutation.removedNodes) {
3321
+ if (node.nodeType !== 1) continue;
3322
+ const el = node;
3323
+ const role = el.getAttribute?.("role");
3324
+ if (role === "dialog" || el.tagName?.toLowerCase() === "dialog") {
3325
+ this._safeCall(() => this.engine.toggle(false));
3326
+ }
3327
+ }
3328
+ }
3329
+ });
3330
+ this._observer.observe(this._root, {
3331
+ childList: true,
3332
+ subtree: true
3333
+ });
3334
+ }
3335
+ _stopObserver() {
3336
+ if (this._observer) {
3337
+ this._observer.disconnect();
3338
+ this._observer = null;
3339
+ }
3340
+ }
3341
+ _safeCall(fn) {
3342
+ try {
3343
+ const result = fn();
3344
+ if (result && typeof result.catch === "function") {
3345
+ result.catch(() => {
3346
+ });
3347
+ }
3348
+ } catch {
3349
+ }
3350
+ }
3351
+ };
3352
+
2625
3353
  // src/index.ts
2626
3354
  var haptic = HapticEngine.create();
2627
3355
 
@@ -2630,21 +3358,29 @@ exports.FallbackManager = FallbackManager;
2630
3358
  exports.HPLParser = HPLParser;
2631
3359
  exports.HPLParserError = HPLParserError;
2632
3360
  exports.HPLTokenizerError = HPLTokenizerError;
3361
+ exports.HapticA11y = HapticA11y;
2633
3362
  exports.HapticEngine = HapticEngine;
3363
+ exports.HapticExperiment = HapticExperiment;
2634
3364
  exports.IoSAudioAdapter = IoSAudioAdapter;
3365
+ exports.MiddlewareManager = MiddlewareManager;
3366
+ exports.MotionDetector = MotionDetector;
2635
3367
  exports.NoopAdapter = NoopAdapter;
2636
3368
  exports.PatternComposer = PatternComposer;
2637
3369
  exports.PatternRecorder = PatternRecorder;
3370
+ exports.ProfileManager = ProfileManager;
3371
+ exports.RhythmSync = RhythmSync;
2638
3372
  exports.SensoryEngine = SensoryEngine;
2639
3373
  exports.SoundEngine = SoundEngine;
2640
3374
  exports.ThemeManager = ThemeManager;
2641
3375
  exports.VisualEngine = VisualEngine;
2642
3376
  exports.WebVibrationAdapter = WebVibrationAdapter;
2643
3377
  exports.accessibility = accessibility;
3378
+ exports.accessibilityBooster = accessibilityBooster;
2644
3379
  exports.bounce = bounce;
2645
3380
  exports.compile = compile;
2646
3381
  exports.detectAdapter = detectAdapter;
2647
3382
  exports.detectPlatform = detectPlatform;
3383
+ exports.durationScaler = durationScaler;
2648
3384
  exports.elastic = elastic;
2649
3385
  exports.emotions = emotions;
2650
3386
  exports.exportPattern = exportPattern;
@@ -2654,16 +3390,21 @@ exports.gravity = gravity;
2654
3390
  exports.haptic = haptic;
2655
3391
  exports.impact = impact;
2656
3392
  exports.importPattern = importPattern;
3393
+ exports.intensityClamper = intensityClamper;
3394
+ exports.intensityScaler = intensityScaler;
2657
3395
  exports.notifications = notifications;
2658
3396
  exports.optimizeSteps = optimizeSteps;
2659
3397
  exports.parseHPL = parseHPL;
2660
3398
  exports.patternFromDataURL = patternFromDataURL;
2661
3399
  exports.patternFromJSON = patternFromJSON;
3400
+ exports.patternRepeater = patternRepeater;
2662
3401
  exports.patternToDataURL = patternToDataURL;
2663
3402
  exports.patternToJSON = patternToJSON;
2664
3403
  exports.pendulum = pendulum;
2665
3404
  exports.physics = physics;
2666
3405
  exports.presets = presets;
3406
+ exports.profiles = profiles;
3407
+ exports.reverser = reverser;
2667
3408
  exports.spring = spring;
2668
3409
  exports.system = system;
2669
3410
  exports.themes = themes;