@scripso-homepad/ui 0.4.38 → 0.4.40

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scripso-homepad/ui",
3
- "version": "0.4.38",
3
+ "version": "0.4.40",
4
4
  "type": "module",
5
5
  "description": "Cross-platform UI components for Homepad (React Web + React Native)",
6
6
  "license": "MIT",
@@ -62,8 +62,7 @@ export const Controlled: Story = {
62
62
  return (
63
63
  <View style={styles.wrapper}>
64
64
  <PhoneInput
65
- label="Label"
66
- placeholder="placeholder"
65
+ label="Հեռախոսահամար"
67
66
  countryCode={countryCode}
68
67
  onCountryCodeChange={setCountryCode}
69
68
  value={phone}
@@ -13,6 +13,7 @@ import {
13
13
  } from "react-native";
14
14
  import { CountryCodeSelector } from "./CountryCodeSelector";
15
15
  import { Label } from "./Label";
16
+ import { defaultCountry } from "../data/countries";
16
17
  import {
17
18
  getInputFieldStyles,
18
19
  inputFieldMetrics,
@@ -22,6 +23,12 @@ import {
22
23
  resolveInputVisualState,
23
24
  } from "../theme/input";
24
25
  import { colors, radii, spacing } from "../theme/tokens";
26
+ import {
27
+ ARMENIAN_PHONE_PLACEHOLDER,
28
+ DEFAULT_PHONE_COUNTRY_CODE,
29
+ formatNationalPhoneDisplay,
30
+ sanitizeNationalPhoneDigits,
31
+ } from "../utils/phoneFormat";
25
32
  import { useApplyWebClassName } from "../utils/useApplyWebClassName";
26
33
 
27
34
  /** Keep country trigger outer box equal to the phone field (transparent focus ring). */
@@ -52,14 +59,9 @@ export interface PhoneInputProps extends Omit<TextInputProps, "style"> {
52
59
  hintClassName?: string;
53
60
  }
54
61
 
55
- /** National phone field: digits only (blocks pasted letters/symbols). */
56
- function sanitizePhoneDigits(value: string): string {
57
- return value.replace(/\D/g, "");
58
- }
59
-
60
62
  export function PhoneInput({
61
63
  label,
62
- countryCode,
64
+ countryCode = defaultCountry.code,
63
65
  onCountryCodeChange,
64
66
  error,
65
67
  hint,
@@ -76,6 +78,8 @@ export function PhoneInput({
76
78
  onFocus,
77
79
  onBlur,
78
80
  onChangeText,
81
+ value,
82
+ placeholder,
79
83
  ...props
80
84
  }: PhoneInputProps) {
81
85
  const wrapperRef = useRef<ComponentRef<typeof View>>(null);
@@ -94,6 +98,16 @@ export function PhoneInput({
94
98
  disabled: isDisabled,
95
99
  });
96
100
  const phoneFieldStyles = getInputFieldStyles(phoneVisualState);
101
+ const resolvedCountryCode = countryCode || DEFAULT_PHONE_COUNTRY_CODE;
102
+ const displayValue =
103
+ typeof value === "string" || typeof value === "number"
104
+ ? formatNationalPhoneDisplay(String(value), resolvedCountryCode)
105
+ : value;
106
+ const resolvedPlaceholder =
107
+ placeholder ??
108
+ (resolvedCountryCode === DEFAULT_PHONE_COUNTRY_CODE
109
+ ? ARMENIAN_PHONE_PLACEHOLDER
110
+ : undefined);
97
111
 
98
112
  function handleFocus(event: NativeSyntheticEvent<TextInputFocusEventData>) {
99
113
  setFocused(true);
@@ -105,8 +119,8 @@ export function PhoneInput({
105
119
  onBlur?.(event);
106
120
  }
107
121
 
108
- function handleChangeText(value: string) {
109
- onChangeText?.(sanitizePhoneDigits(value));
122
+ function handleChangeText(nextValue: string) {
123
+ onChangeText?.(sanitizeNationalPhoneDigits(nextValue, resolvedCountryCode));
110
124
  }
111
125
 
112
126
  const helperMessage = error ?? hint;
@@ -158,6 +172,8 @@ export function PhoneInput({
158
172
  onBlur={handleBlur}
159
173
  accessibilityState={{ disabled: isDisabled }}
160
174
  {...props}
175
+ value={displayValue}
176
+ placeholder={resolvedPlaceholder}
161
177
  onChangeText={handleChangeText}
162
178
  />
163
179
  </View>
package/src/index.ts CHANGED
@@ -19,6 +19,15 @@ export type {
19
19
  export { PhoneInput } from "./components/PhoneInput";
20
20
  export type { PhoneInputProps } from "./components/PhoneInput";
21
21
 
22
+ export {
23
+ ARMENIAN_PHONE_PLACEHOLDER,
24
+ DEFAULT_PHONE_COUNTRY_CODE,
25
+ formatArmenianNationalNumber,
26
+ formatNationalPhoneDisplay,
27
+ getPhoneDigits,
28
+ sanitizeNationalPhoneDigits,
29
+ } from "./utils/phoneFormat";
30
+
22
31
  export { CountryCodeSelector } from "./components/CountryCodeSelector";
23
32
  export type { CountryCodeSelectorProps } from "./components/CountryCodeSelector";
24
33
 
@@ -0,0 +1,34 @@
1
+ import assert from 'node:assert/strict';
2
+ import { describe, it } from 'node:test';
3
+
4
+ import {
5
+ ARMENIAN_PHONE_PLACEHOLDER,
6
+ formatArmenianNationalNumber,
7
+ formatNationalPhoneDisplay,
8
+ sanitizeNationalPhoneDigits,
9
+ } from './phoneFormat';
10
+
11
+ describe('phoneFormat', () => {
12
+ it('formats Armenia national numbers while typing', () => {
13
+ assert.equal(formatArmenianNationalNumber('9'), '(9');
14
+ assert.equal(formatArmenianNationalNumber('91'), '(91');
15
+ assert.equal(formatArmenianNationalNumber('914'), '(91) 4');
16
+ assert.equal(formatArmenianNationalNumber('9144'), '(91) 44');
17
+ assert.equal(formatArmenianNationalNumber('914455'), '(91) 44-55');
18
+ assert.equal(formatArmenianNationalNumber('91445566'), '(91) 44-55-66');
19
+ });
20
+
21
+ it('caps Armenia national numbers at 8 digits', () => {
22
+ assert.equal(sanitizeNationalPhoneDigits('9144556699', 'AM'), '91445566');
23
+ assert.equal(formatNationalPhoneDisplay('9144556699', 'AM'), '(91) 44-55-66');
24
+ });
25
+
26
+ it('leaves non-Armenia numbers as digits', () => {
27
+ assert.equal(formatNationalPhoneDisplay('555123456', 'GE'), '555123456');
28
+ assert.equal(sanitizeNationalPhoneDigits('(555) 123-456', 'GE'), '555123456');
29
+ });
30
+
31
+ it('exposes the Armenia placeholder mask', () => {
32
+ assert.equal(ARMENIAN_PHONE_PLACEHOLDER, '(00) 00-00-00');
33
+ });
34
+ });
@@ -0,0 +1,57 @@
1
+ import { defaultCountry } from "../data/countries";
2
+
3
+ export const DEFAULT_PHONE_COUNTRY_CODE = defaultCountry.code;
4
+ const AM_NATIONAL_MAX_DIGITS = 8;
5
+ const MAX_NATIONAL_DIGITS = 15;
6
+
7
+ export function getPhoneDigits(value: string): string {
8
+ return value.replace(/\D/g, "");
9
+ }
10
+
11
+ /** Armenia mobile display while typing: (91) 44-55-66 */
12
+ export function formatArmenianNationalNumber(digits: string): string {
13
+ const national = getPhoneDigits(digits).slice(0, AM_NATIONAL_MAX_DIGITS);
14
+
15
+ if (national.length === 0) {
16
+ return "";
17
+ }
18
+
19
+ if (national.length <= 2) {
20
+ return `(${national}`;
21
+ }
22
+
23
+ const head = national.slice(0, 2);
24
+ const rest = national.slice(2);
25
+ const groups = rest.match(/.{1,2}/g) ?? [];
26
+
27
+ return `(${head}) ${groups.join("-")}`;
28
+ }
29
+
30
+ export function formatNationalPhoneDisplay(
31
+ digits: string,
32
+ countryCode: string = DEFAULT_PHONE_COUNTRY_CODE,
33
+ ): string {
34
+ const national = getPhoneDigits(digits).slice(0, MAX_NATIONAL_DIGITS);
35
+
36
+ if (countryCode === DEFAULT_PHONE_COUNTRY_CODE) {
37
+ return formatArmenianNationalNumber(national);
38
+ }
39
+
40
+ return national;
41
+ }
42
+
43
+ export function sanitizeNationalPhoneDigits(
44
+ value: string,
45
+ countryCode: string = DEFAULT_PHONE_COUNTRY_CODE,
46
+ ): string {
47
+ const digits = getPhoneDigits(value);
48
+
49
+ if (countryCode === DEFAULT_PHONE_COUNTRY_CODE) {
50
+ return digits.slice(0, AM_NATIONAL_MAX_DIGITS);
51
+ }
52
+
53
+ return digits.slice(0, MAX_NATIONAL_DIGITS);
54
+ }
55
+
56
+ /** Placeholder mask shown for Armenia national numbers. */
57
+ export const ARMENIAN_PHONE_PLACEHOLDER = "(00) 00-00-00";
@@ -1,6 +1,7 @@
1
1
  import { useEffect, type ReactNode } from 'react';
2
2
 
3
3
  import { Button } from '../../components/Button';
4
+ import { useBackdropDismiss } from '../hooks/useBackdropDismiss';
4
5
  import { cn } from '../utils/cn';
5
6
 
6
7
  export type ConfirmModalProps = {
@@ -35,6 +36,8 @@ export function ConfirmModal({
35
36
  containerClassName,
36
37
  buttonClassName,
37
38
  }: ConfirmModalProps) {
39
+ const backdropDismiss = useBackdropDismiss(onCancel, closeOnBackdrop);
40
+
38
41
  useEffect(() => {
39
42
  if (!open) {
40
43
  return;
@@ -65,7 +68,8 @@ export function ConfirmModal({
65
68
  return (
66
69
  <div
67
70
  className="fixed inset-0 z-50 flex items-center justify-center p-4 backdrop-blur-md bg-storm-gray-850/40"
68
- onClick={closeOnBackdrop ? onCancel : undefined}
71
+ onPointerDown={backdropDismiss.onPointerDown}
72
+ onClick={backdropDismiss.onClick}
69
73
  role="presentation"
70
74
  >
71
75
  <div
@@ -77,9 +81,6 @@ export function ConfirmModal({
77
81
  'flex w-full max-w-md flex-col gap-8 rounded-3xl bg-white p-6 opacity-100 md:p-8',
78
82
  containerClassName,
79
83
  )}
80
- onClick={(event) => {
81
- event.stopPropagation();
82
- }}
83
84
  >
84
85
  <header className="flex flex-col items-center gap-3 text-center">
85
86
  {icon ? (
@@ -1,6 +1,7 @@
1
1
  import { X } from 'lucide-react';
2
2
  import { useEffect, useState, type ReactNode } from 'react';
3
3
 
4
+ import { useBackdropDismiss } from '../hooks/useBackdropDismiss';
4
5
  import { cn } from '../utils/cn';
5
6
 
6
7
  import './drawer-scroll.css';
@@ -72,6 +73,7 @@ export function Drawer({
72
73
  const [isPresent, setIsPresent] = useState(open);
73
74
  const [isAnimatedIn, setIsAnimatedIn] = useState(false);
74
75
  const [isTransformSettled, setIsTransformSettled] = useState(open);
76
+ const backdropDismiss = useBackdropDismiss(onClose);
75
77
 
76
78
  useEffect(() => {
77
79
  if (open) {
@@ -151,7 +153,8 @@ export function Drawer({
151
153
  'absolute inset-0 z-0 cursor-pointer bg-storm-gray-850/40 backdrop-blur-[12px] transition-opacity duration-300 ease-in-out',
152
154
  isAnimatedIn ? 'opacity-100' : 'opacity-0',
153
155
  )}
154
- onClick={onClose}
156
+ onPointerDown={backdropDismiss.onPointerDown}
157
+ onClick={backdropDismiss.onClick}
155
158
  />
156
159
 
157
160
  <aside
@@ -2,6 +2,7 @@ import { useEffect, type ReactNode } from 'react';
2
2
  import { createPortal } from 'react-dom';
3
3
 
4
4
  import { Button } from '../../components/Button';
5
+ import { useBackdropDismiss } from '../hooks/useBackdropDismiss';
5
6
  import { cn } from '../utils/cn';
6
7
  import './drawer-scroll.css';
7
8
 
@@ -61,6 +62,8 @@ export function Modal({
61
62
  contentClassName,
62
63
  buttonClassName,
63
64
  }: ModalProps) {
65
+ const backdropDismiss = useBackdropDismiss(onCancel, closeOnBackdrop);
66
+
64
67
  useEffect(() => {
65
68
  if (!open) {
66
69
  return;
@@ -95,15 +98,8 @@ export function Modal({
95
98
  const modal = (
96
99
  <div
97
100
  className="fixed inset-0 z-[100] flex items-center justify-center p-4 backdrop-blur-[12px] bg-storm-gray-850/40"
98
- onClick={
99
- closeOnBackdrop
100
- ? (event) => {
101
- if (event.target === event.currentTarget) {
102
- onCancel();
103
- }
104
- }
105
- : undefined
106
- }
101
+ onPointerDown={backdropDismiss.onPointerDown}
102
+ onClick={backdropDismiss.onClick}
107
103
  role="presentation"
108
104
  >
109
105
  <div
@@ -115,9 +111,6 @@ export function Modal({
115
111
  'flex max-h-[min(90dvh,calc(100dvh-2rem))] w-full max-w-2xl flex-col gap-6 rounded-2xl bg-white p-4 opacity-100 md:p-6',
116
112
  containerClassName,
117
113
  )}
118
- onClick={(event) => {
119
- event.stopPropagation();
120
- }}
121
114
  >
122
115
  <header className={cn('flex shrink-0', icon ? 'items-center gap-2.5' : undefined)}>
123
116
  {icon ? (
@@ -0,0 +1,44 @@
1
+ import { useCallback, useRef, type MouseEvent, type PointerEvent } from 'react';
2
+
3
+ type BackdropDismissHandlers = {
4
+ onPointerDown: (event: PointerEvent<HTMLElement>) => void;
5
+ onClick: (event: MouseEvent<HTMLElement>) => void;
6
+ };
7
+
8
+ /**
9
+ * Dismiss only on a true outside click: pointer must both press and release
10
+ * on the backdrop. Dragging from inside the dialog and releasing outside
11
+ * must not close.
12
+ */
13
+ export function useBackdropDismiss(
14
+ onDismiss: () => void,
15
+ enabled = true,
16
+ ): BackdropDismissHandlers {
17
+ const pointerDownOnBackdropRef = useRef(false);
18
+
19
+ const onPointerDown = useCallback(
20
+ (event: PointerEvent<HTMLElement>) => {
21
+ pointerDownOnBackdropRef.current =
22
+ enabled && event.target === event.currentTarget;
23
+ },
24
+ [enabled],
25
+ );
26
+
27
+ const onClick = useCallback(
28
+ (event: MouseEvent<HTMLElement>) => {
29
+ const shouldDismiss =
30
+ enabled &&
31
+ pointerDownOnBackdropRef.current &&
32
+ event.target === event.currentTarget;
33
+
34
+ pointerDownOnBackdropRef.current = false;
35
+
36
+ if (shouldDismiss) {
37
+ onDismiss();
38
+ }
39
+ },
40
+ [enabled, onDismiss],
41
+ );
42
+
43
+ return { onPointerDown, onClick };
44
+ }
package/src/web/index.ts CHANGED
@@ -128,6 +128,7 @@ export type { ConfirmModalProps } from './components/ConfirmModal';
128
128
 
129
129
  export { useMediaQuery } from './hooks/useMediaQuery';
130
130
  export { useOnClickOutside } from './hooks/useOnClickOutside';
131
+ export { useBackdropDismiss } from './hooks/useBackdropDismiss';
131
132
  export { cn } from './utils/cn';
132
133
 
133
134
  export type {