glacia-native 0.5.0 → 0.5.1
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/lib/module/Bars.js +95 -2
- package/lib/module/Display.js +78 -1
- package/lib/module/Feedback.js +221 -7
- package/lib/module/Forms2.js +228 -1
- package/lib/module/Message.js +88 -0
- package/lib/module/Overlays.js +173 -11
- package/lib/module/SegmentedControl.js +5 -1
- package/lib/module/Slider.js +6 -1
- package/lib/module/Switch.js +4 -1
- package/lib/module/TabBar.js +5 -1
- package/lib/module/ai.js +8 -3
- package/lib/module/index.js +23 -2
- package/lib/module/internal/GlassContainer.js +54 -0
- package/lib/module/internal/GlassView.js +8 -0
- package/lib/module/internal/a11y.js +94 -0
- package/lib/module/internal/fallback.js +19 -8
- package/lib/module/internal/glass-routing.js +7 -0
- package/lib/module/internal/haptics.js +54 -0
- package/lib/module/internal/motion.js +10 -1
- package/lib/module/internal/primitives.js +69 -4
- package/lib/module/internal/tokens.js +47 -1
- package/lib/typescript/Bars.d.ts +67 -0
- package/lib/typescript/Display.d.ts +59 -0
- package/lib/typescript/Feedback.d.ts +58 -0
- package/lib/typescript/Forms2.d.ts +81 -0
- package/lib/typescript/Message.d.ts +71 -0
- package/lib/typescript/Overlays.d.ts +41 -0
- package/lib/typescript/SegmentedControl.d.ts +2 -0
- package/lib/typescript/Slider.d.ts +2 -0
- package/lib/typescript/Switch.d.ts +2 -0
- package/lib/typescript/TabBar.d.ts +2 -0
- package/lib/typescript/index.d.ts +8 -6
- package/lib/typescript/internal/GlassContainer.d.ts +37 -0
- package/lib/typescript/internal/GlassView.d.ts +8 -0
- package/lib/typescript/internal/a11y.d.ts +19 -0
- package/lib/typescript/internal/fallback.d.ts +5 -0
- package/lib/typescript/internal/glass-routing.d.ts +10 -0
- package/lib/typescript/internal/haptics.d.ts +7 -0
- package/lib/typescript/internal/motion.d.ts +4 -0
- package/lib/typescript/internal/primitives.d.ts +21 -0
- package/lib/typescript/internal/tokens.d.ts +53 -0
- package/package.json +2 -2
- package/src/Bars.tsx +227 -0
- package/src/Display.tsx +229 -1
- package/src/Feedback.tsx +412 -22
- package/src/Forms2.tsx +407 -1
- package/src/Message.tsx +219 -0
- package/src/Overlays.tsx +244 -33
- package/src/SegmentedControl.tsx +6 -0
- package/src/Slider.tsx +7 -0
- package/src/Switch.tsx +5 -0
- package/src/TabBar.tsx +6 -0
- package/src/ai.tsx +9 -3
- package/src/index.ts +42 -1
- package/src/internal/GlassContainer.tsx +82 -0
- package/src/internal/GlassView.tsx +8 -0
- package/src/internal/a11y.ts +105 -0
- package/src/internal/fallback.tsx +19 -2
- package/src/internal/glass-routing.ts +17 -0
- package/src/internal/haptics.ts +59 -0
- package/src/internal/motion.ts +10 -1
- package/src/internal/primitives.ts +80 -4
- package/src/internal/tokens.ts +48 -0
package/src/Forms2.tsx
CHANGED
|
@@ -3,7 +3,7 @@ import type { StyleProp, ViewStyle } from "react-native";
|
|
|
3
3
|
|
|
4
4
|
import { Glass } from "./Glass";
|
|
5
5
|
import { Text } from "./Text";
|
|
6
|
-
import { colors, radii } from "./internal/tokens";
|
|
6
|
+
import { colors, controlSizes, palette as tokenPalette, radii } from "./internal/tokens";
|
|
7
7
|
import { primitives } from "./internal/primitives";
|
|
8
8
|
import { useIsDark } from "./internal/motion";
|
|
9
9
|
|
|
@@ -606,3 +606,409 @@ export const ButtonGroup = forwardRef<any, ButtonGroupProps>(function ButtonGrou
|
|
|
606
606
|
</View>
|
|
607
607
|
);
|
|
608
608
|
});
|
|
609
|
+
|
|
610
|
+
// ---------------------------------------------------------------------------
|
|
611
|
+
// ColorPicker
|
|
612
|
+
|
|
613
|
+
export interface ColorPickerProps {
|
|
614
|
+
/** A palette of hex colours the caller supplies. */
|
|
615
|
+
colors?: string[];
|
|
616
|
+
/** Controlled value. Omit and the picker keeps its own. */
|
|
617
|
+
value?: string;
|
|
618
|
+
defaultValue?: string;
|
|
619
|
+
onChange?: (color: string) => void;
|
|
620
|
+
/** The swatch size. */
|
|
621
|
+
size?: "sm" | "md" | "lg";
|
|
622
|
+
/** Rows in the palette grid. Defaults to a single row. */
|
|
623
|
+
columns?: number;
|
|
624
|
+
/** Whether a custom row (hue + picker) appears below the palette. */
|
|
625
|
+
allowCustom?: boolean;
|
|
626
|
+
disabled?: boolean;
|
|
627
|
+
/** The accessible name. */
|
|
628
|
+
accessibilityLabel?: string;
|
|
629
|
+
style?: StyleProp<ViewStyle>;
|
|
630
|
+
isDark?: boolean;
|
|
631
|
+
testID?: string;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* A colour picker built from Glacia's own parts — no native widget.
|
|
636
|
+
*
|
|
637
|
+
* The palette is a grid of swatches from the caller's colours; the custom row
|
|
638
|
+
* is a hue track drawn as an SVG gradient (the resolver's shapes, so it works
|
|
639
|
+
* wherever the charts work) driven by the same Slider mechanics as the rest of
|
|
640
|
+
* the library: a drag along the track picks a hue, and a second track saturates
|
|
641
|
+
* it. The value is a hex string.
|
|
642
|
+
*
|
|
643
|
+
* The default palette is a small set of iOS-conventional colours, in case a
|
|
644
|
+
* caller wants a picker with no colour knowledge of its own.
|
|
645
|
+
*/
|
|
646
|
+
export const DEFAULT_PALETTE = tokenPalette.default;
|
|
647
|
+
|
|
648
|
+
/** Parses a hex colour into its red, green and blue channels. */
|
|
649
|
+
export function hexToRgb(hex: string): { r: number; g: number; b: number } | null {
|
|
650
|
+
const match = /^#?([0-9a-f]{6})$/i.exec(hex.trim());
|
|
651
|
+
if (!match) return null;
|
|
652
|
+
const raw = match[1]!;
|
|
653
|
+
return {
|
|
654
|
+
r: parseInt(raw.slice(0, 2), 16),
|
|
655
|
+
g: parseInt(raw.slice(2, 4), 16),
|
|
656
|
+
b: parseInt(raw.slice(4, 6), 16),
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/** Builds a hue gradient's colours, sweeping red → orange → … → red. */
|
|
661
|
+
export function hueGradientColors(): string[] {
|
|
662
|
+
const stops = 12;
|
|
663
|
+
return Array.from({ length: stops }, (_u, i) => {
|
|
664
|
+
const angle = (i / stops) * 360;
|
|
665
|
+
const c = hslToRgb(angle, 1, 0.5);
|
|
666
|
+
return `#${c.r.toString(16).padStart(2, "0")}${c.g.toString(16).padStart(2, "0")}${c.b.toString(16).padStart(2, "0")}`;
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** Converts an HSL triple (h 0–360, s/l 0–1) to an RGB hex string. */
|
|
671
|
+
function hslToRgb(h: number, s: number, l: number): { r: number; g: number; b: number } {
|
|
672
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
673
|
+
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
|
|
674
|
+
const m = l - c / 2;
|
|
675
|
+
let r = 0, g = 0, b = 0;
|
|
676
|
+
if (h < 60) { r = c; g = x; } else if (h < 120) { r = x; g = c; } else if (h < 180) { g = c; b = x; } else if (h < 240) { g = x; b = c; } else if (h < 300) { r = x; b = c; } else { r = c; b = x; }
|
|
677
|
+
return {
|
|
678
|
+
r: Math.round((r + m) * 255),
|
|
679
|
+
g: Math.round((g + m) * 255),
|
|
680
|
+
b: Math.round((b + m) * 255),
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/** Converts an RGB triple to a hex string. */
|
|
685
|
+
export function rgbToHex(r: number, g: number, b: number): string {
|
|
686
|
+
const clamp = (n: number) => Math.max(0, Math.min(255, Math.round(n)));
|
|
687
|
+
return `#${clamp(r).toString(16).padStart(2, "0")}${clamp(g).toString(16).padStart(2, "0")}${clamp(b).toString(16).padStart(2, "0")}`;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
export const ColorPicker = forwardRef<any, ColorPickerProps>(function ColorPicker(
|
|
691
|
+
{
|
|
692
|
+
colors: palette,
|
|
693
|
+
value,
|
|
694
|
+
defaultValue,
|
|
695
|
+
onChange,
|
|
696
|
+
size = "md",
|
|
697
|
+
columns = 6,
|
|
698
|
+
allowCustom = true,
|
|
699
|
+
disabled = false,
|
|
700
|
+
accessibilityLabel = "Color",
|
|
701
|
+
style,
|
|
702
|
+
isDark: isDarkProp,
|
|
703
|
+
testID,
|
|
704
|
+
...props
|
|
705
|
+
},
|
|
706
|
+
ref,
|
|
707
|
+
) {
|
|
708
|
+
const isDark = useIsDark(isDarkProp);
|
|
709
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
710
|
+
const { View, Pressable, Text: RNText, PanResponder, Svg, Rect, Defs, LinearGradient, Stop } = primitives();
|
|
711
|
+
|
|
712
|
+
const swatches = palette && palette.length > 0 ? palette : DEFAULT_PALETTE;
|
|
713
|
+
|
|
714
|
+
const [internalValue, setInternalValue] = useState(defaultValue ?? swatches[0] ?? tokenPalette.fallback);
|
|
715
|
+
const activeColor = value !== undefined ? value : internalValue;
|
|
716
|
+
|
|
717
|
+
const setColor = (next: string) => {
|
|
718
|
+
if (disabled) return;
|
|
719
|
+
if (value === undefined) setInternalValue(next);
|
|
720
|
+
onChange?.(next);
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
// The custom row's two tracks: hue on the top, saturation on the bottom.
|
|
724
|
+
// Both are driven by PanResponder like the library's Slider — a press or a
|
|
725
|
+
// drag along a track reports the colour at that position.
|
|
726
|
+
const trackRef = useRef(0);
|
|
727
|
+
const swatchSize = controlSizes.swatch[size];
|
|
728
|
+
|
|
729
|
+
const hueColor = (h: number) => {
|
|
730
|
+
const c = hslToRgb(h, 1, 0.5);
|
|
731
|
+
return rgbToHex(c.r, c.g, c.b);
|
|
732
|
+
};
|
|
733
|
+
|
|
734
|
+
const colorAtFraction = (fraction: number, hue: number) => {
|
|
735
|
+
const sat = Math.max(0, Math.min(1, fraction));
|
|
736
|
+
const c = hslToRgb(hue, sat, 0.5);
|
|
737
|
+
return rgbToHex(c.r, c.g, c.b);
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
const hue = hexToRgb(activeColor) ? hueOf(activeColor) : 0;
|
|
741
|
+
|
|
742
|
+
const hueResponder = makeTrackResponder((fraction) => {
|
|
743
|
+
setColor(hueColor(fraction * 360));
|
|
744
|
+
}, PanResponder, trackRef);
|
|
745
|
+
|
|
746
|
+
const satResponder = makeTrackResponder((fraction) => {
|
|
747
|
+
setColor(colorAtFraction(fraction, hue));
|
|
748
|
+
}, PanResponder, trackRef);
|
|
749
|
+
|
|
750
|
+
const hueGradient = hueGradientColors();
|
|
751
|
+
|
|
752
|
+
return (
|
|
753
|
+
<View
|
|
754
|
+
ref={ref}
|
|
755
|
+
testID={testID}
|
|
756
|
+
accessibilityRole="adjustable"
|
|
757
|
+
accessibilityLabel={accessibilityLabel}
|
|
758
|
+
accessibilityValue={{ text: activeColor }}
|
|
759
|
+
style={{ gap: 12, opacity: disabled ? 0.38 : 1, ...(style as ViewStyle) }}
|
|
760
|
+
{...props}
|
|
761
|
+
>
|
|
762
|
+
{/* The palette grid. */}
|
|
763
|
+
<View style={{ flexDirection: "row", flexWrap: "wrap", gap: 8 }}>
|
|
764
|
+
{swatches.map((swatch, index) => {
|
|
765
|
+
const selected = swatch.toLowerCase() === activeColor.toLowerCase();
|
|
766
|
+
return (
|
|
767
|
+
<Pressable
|
|
768
|
+
key={`${swatch}-${index}`}
|
|
769
|
+
accessibilityRole="button"
|
|
770
|
+
accessibilityLabel={`${swatch} color`}
|
|
771
|
+
accessibilityState={{ selected }}
|
|
772
|
+
disabled={disabled}
|
|
773
|
+
onPress={() => setColor(swatch)}
|
|
774
|
+
style={{
|
|
775
|
+
width: swatchSize,
|
|
776
|
+
height: swatchSize,
|
|
777
|
+
borderRadius: swatchSize / 2,
|
|
778
|
+
backgroundColor: swatch,
|
|
779
|
+
borderWidth: selected ? 2 : 0.5,
|
|
780
|
+
borderColor: selected ? theme.fill : theme.separator,
|
|
781
|
+
}}
|
|
782
|
+
/>
|
|
783
|
+
);
|
|
784
|
+
})}
|
|
785
|
+
</View>
|
|
786
|
+
|
|
787
|
+
{allowCustom ? (
|
|
788
|
+
<View style={{ gap: 8 }}>
|
|
789
|
+
{/* Hue track */}
|
|
790
|
+
<View
|
|
791
|
+
style={{ height: controlSizes.picker.track, borderRadius: radii.md, overflow: "hidden" }}
|
|
792
|
+
{...(hueResponder ? hueResponder.panHandlers : {})}
|
|
793
|
+
>
|
|
794
|
+
<Svg width="100%" height="100%">
|
|
795
|
+
<Defs>
|
|
796
|
+
<LinearGradient id="hueGradient" x1="0" y1="0" x2="1" y2="0">
|
|
797
|
+
{hueGradient.map((color, i) => (
|
|
798
|
+
<Stop key={i} offset={i / (hueGradient.length - 1)} stopColor={color} />
|
|
799
|
+
))}
|
|
800
|
+
</LinearGradient>
|
|
801
|
+
</Defs>
|
|
802
|
+
<Rect x="0" y="0" width="100%" height="100%" fill="url(#hueGradient)" />
|
|
803
|
+
</Svg>
|
|
804
|
+
</View>
|
|
805
|
+
{/* Saturation track: the hue at full saturation fading to white. */}
|
|
806
|
+
<View
|
|
807
|
+
style={{ height: controlSizes.picker.track, borderRadius: radii.md, overflow: "hidden" }}
|
|
808
|
+
{...(satResponder ? satResponder.panHandlers : {})}
|
|
809
|
+
>
|
|
810
|
+
<Svg width="100%" height="100%">
|
|
811
|
+
<Defs>
|
|
812
|
+
<LinearGradient id="satGradient" x1="0" y1="0" x2="1" y2="0">
|
|
813
|
+
<Stop offset="0" stopColor={theme.neutralTop} />
|
|
814
|
+
<Stop offset="1" stopColor={hueColor(hue)} />
|
|
815
|
+
</LinearGradient>
|
|
816
|
+
</Defs>
|
|
817
|
+
<Rect x="0" y="0" width="100%" height="100%" fill="url(#satGradient)" />
|
|
818
|
+
</Svg>
|
|
819
|
+
</View>
|
|
820
|
+
<RNText style={{ fontSize: 12, color: theme.muted }}>{activeColor}</RNText>
|
|
821
|
+
</View>
|
|
822
|
+
) : null}
|
|
823
|
+
</View>
|
|
824
|
+
);
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
/** The hue (0–360) of a hex colour. */
|
|
828
|
+
function hueOf(hex: string): number {
|
|
829
|
+
const rgb = hexToRgb(hex);
|
|
830
|
+
if (!rgb) return 0;
|
|
831
|
+
const { r, g, b } = rgb;
|
|
832
|
+
const max = Math.max(r, g, b) / 255;
|
|
833
|
+
const min = Math.min(r, g, b) / 255;
|
|
834
|
+
const delta = max - min;
|
|
835
|
+
if (delta === 0) return 0;
|
|
836
|
+
let h = 0;
|
|
837
|
+
if (max === r / 255) h = ((g / 255 - b / 255) / delta) % 6;
|
|
838
|
+
else if (max === g / 255) h = (b / 255 - r / 255) / delta + 2;
|
|
839
|
+
else h = (r / 255 - g / 255) / delta + 4;
|
|
840
|
+
const degrees = h * 60;
|
|
841
|
+
return degrees < 0 ? degrees + 360 : degrees;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/** Builds a PanResponder that reports the fraction (0–1) across a track. */
|
|
845
|
+
function makeTrackResponder(
|
|
846
|
+
onFraction: (fraction: number) => void,
|
|
847
|
+
PanResponder: any,
|
|
848
|
+
trackRef: { current: number },
|
|
849
|
+
): any {
|
|
850
|
+
if (!PanResponder?.create) return null;
|
|
851
|
+
return PanResponder.create({
|
|
852
|
+
onStartShouldSetPanResponder: () => true,
|
|
853
|
+
onMoveShouldSetPanResponder: () => true,
|
|
854
|
+
onPanResponderGrant: (event: any) => {
|
|
855
|
+
const fraction = fractionAt(event, 100);
|
|
856
|
+
if (fraction !== null) onFraction(fraction);
|
|
857
|
+
},
|
|
858
|
+
onPanResponderMove: (event: any) => {
|
|
859
|
+
const fraction = fractionAt(event, 100);
|
|
860
|
+
if (fraction !== null) onFraction(fraction);
|
|
861
|
+
},
|
|
862
|
+
onPanResponderTerminationRequest: () => false,
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/** The fraction of the track under a touch, from the touch's X. */
|
|
867
|
+
function fractionAt(event: any, fallbackWidth: number): number | null {
|
|
868
|
+
const x = event?.nativeEvent?.locationX;
|
|
869
|
+
const width = event?.nativeEvent?.layoutMeasurement?.width ?? fallbackWidth;
|
|
870
|
+
if (typeof x !== "number" || typeof width !== "number" || width <= 0) return null;
|
|
871
|
+
return Math.max(0, Math.min(1, x / width));
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// ---------------------------------------------------------------------------
|
|
875
|
+
// Combobox
|
|
876
|
+
|
|
877
|
+
export interface ComboboxOption {
|
|
878
|
+
label: string;
|
|
879
|
+
value: string;
|
|
880
|
+
/** Extra text under the label, if any. */
|
|
881
|
+
description?: string;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
export interface ComboboxProps {
|
|
885
|
+
/** The options the user can choose from. */
|
|
886
|
+
options: ComboboxOption[];
|
|
887
|
+
/** Controlled value. Omit and the combobox keeps its own. */
|
|
888
|
+
value?: string;
|
|
889
|
+
defaultValue?: string;
|
|
890
|
+
onChange?: (value: string) => void;
|
|
891
|
+
/** A description of the underlying field. */
|
|
892
|
+
label?: string;
|
|
893
|
+
placeholder?: string;
|
|
894
|
+
disabled?: boolean;
|
|
895
|
+
/** Called when the dropdown's empty result shows. */
|
|
896
|
+
onEmpty?: () => void;
|
|
897
|
+
style?: StyleProp<ViewStyle>;
|
|
898
|
+
isDark?: boolean;
|
|
899
|
+
testID?: string;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
/**
|
|
903
|
+
* A type-to-search select.
|
|
904
|
+
*
|
|
905
|
+
* The Input patterns (real TextInput, themed placeholder, `value`/`defaultValue`)
|
|
906
|
+
* open a Menu-style glass dropdown whose list filters as the user types — every
|
|
907
|
+
* keystroke re-filters the options, so the visible set genuinely shrinks, never
|
|
908
|
+
* a list that stays put and pretends to search.
|
|
909
|
+
*
|
|
910
|
+
* The filter is a case-insensitive substring match on label and description. An
|
|
911
|
+
* empty result shows an explicit empty row, so the user sees a search that found
|
|
912
|
+
* nothing rather than a list that silently went blank. The dropdown stays open
|
|
913
|
+
* until an option is picked or the field loses focus.
|
|
914
|
+
*/
|
|
915
|
+
export const Combobox = forwardRef<any, ComboboxProps>(function Combobox(
|
|
916
|
+
{
|
|
917
|
+
options,
|
|
918
|
+
value,
|
|
919
|
+
defaultValue,
|
|
920
|
+
onChange,
|
|
921
|
+
label,
|
|
922
|
+
placeholder = "Search…",
|
|
923
|
+
disabled = false,
|
|
924
|
+
onEmpty,
|
|
925
|
+
style,
|
|
926
|
+
isDark: isDarkProp,
|
|
927
|
+
testID,
|
|
928
|
+
...props
|
|
929
|
+
},
|
|
930
|
+
ref,
|
|
931
|
+
) {
|
|
932
|
+
const isDark = useIsDark(isDarkProp);
|
|
933
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
934
|
+
const { View, TextInput, Text: RNText, Pressable } = primitives();
|
|
935
|
+
|
|
936
|
+
const [internalValue, setInternalValue] = useState(defaultValue ?? "");
|
|
937
|
+
const [query, setQuery] = useState("");
|
|
938
|
+
|
|
939
|
+
const selected = value !== undefined ? value : internalValue;
|
|
940
|
+
const selectedLabel = (options ?? []).find((option) => option.value === selected)?.label ?? "";
|
|
941
|
+
|
|
942
|
+
const setValue = (next: string) => {
|
|
943
|
+
if (disabled) return;
|
|
944
|
+
if (value === undefined) setInternalValue(next);
|
|
945
|
+
onChange?.(next);
|
|
946
|
+
};
|
|
947
|
+
|
|
948
|
+
// Filter as the user types.
|
|
949
|
+
// An option list that has not loaded yet is empty, not a crash: a combobox
|
|
950
|
+
// is frequently rendered before its options arrive.
|
|
951
|
+
const available = options ?? [];
|
|
952
|
+
const needle = query.trim().toLowerCase();
|
|
953
|
+
const matches = needle.length === 0
|
|
954
|
+
? available
|
|
955
|
+
: available.filter((option) =>
|
|
956
|
+
option.label.toLowerCase().includes(needle) ||
|
|
957
|
+
(option.description ?? "").toLowerCase().includes(needle),
|
|
958
|
+
);
|
|
959
|
+
|
|
960
|
+
return (
|
|
961
|
+
<View ref={ref} testID={testID} style={{ ...(style as ViewStyle) }}>
|
|
962
|
+
<TextInput
|
|
963
|
+
value={query}
|
|
964
|
+
onChangeText={(text: string) => setQuery(text)}
|
|
965
|
+
placeholder={placeholder}
|
|
966
|
+
accessibilityLabel={label ?? placeholder}
|
|
967
|
+
accessibilityState={{ disabled }}
|
|
968
|
+
editable={!disabled}
|
|
969
|
+
style={{
|
|
970
|
+
borderRadius: radii.md,
|
|
971
|
+
paddingHorizontal: 12,
|
|
972
|
+
paddingVertical: 8,
|
|
973
|
+
fontSize: 16,
|
|
974
|
+
color: theme.foreground,
|
|
975
|
+
backgroundColor: disabled ? theme.muted : theme.fieldWash,
|
|
976
|
+
}}
|
|
977
|
+
/>
|
|
978
|
+
{/* The dropdown: opens once there is something to match. */}
|
|
979
|
+
{query.length > 0 ? (
|
|
980
|
+
<View style={{ marginTop: 6, borderRadius: radii.md, overflow: "hidden", backgroundColor: disabled ? theme.muted : theme.glass }}>
|
|
981
|
+
{matches.length === 0 ? (
|
|
982
|
+
<View style={{ paddingHorizontal: 12, paddingVertical: 10 }}>
|
|
983
|
+
<RNText style={{ fontSize: 14, color: theme.muted }}>No matches</RNText>
|
|
984
|
+
</View>
|
|
985
|
+
) : (
|
|
986
|
+
matches.map((option, index) => {
|
|
987
|
+
const isSelected = option.value === selected;
|
|
988
|
+
return (
|
|
989
|
+
<Pressable
|
|
990
|
+
key={`${option.value}-${index}`}
|
|
991
|
+
accessibilityRole="button"
|
|
992
|
+
accessibilityLabel={option.label}
|
|
993
|
+
accessibilityState={{ selected: isSelected }}
|
|
994
|
+
onPress={() => {
|
|
995
|
+
setValue(option.value);
|
|
996
|
+
setQuery("");
|
|
997
|
+
}}
|
|
998
|
+
style={{ flexDirection: "row", alignItems: "center", paddingHorizontal: 12, paddingVertical: 10, backgroundColor: isSelected ? theme.glassActive : "transparent" }}
|
|
999
|
+
>
|
|
1000
|
+
<View style={{ flex: 1 }}>
|
|
1001
|
+
<RNText style={{ fontSize: 15, color: theme.foreground }}>{option.label}</RNText>
|
|
1002
|
+
{option.description ? (
|
|
1003
|
+
<RNText style={{ fontSize: 12, color: theme.muted }} numberOfLines={1}>{option.description}</RNText>
|
|
1004
|
+
) : null}
|
|
1005
|
+
</View>
|
|
1006
|
+
</Pressable>
|
|
1007
|
+
);
|
|
1008
|
+
})
|
|
1009
|
+
)}
|
|
1010
|
+
</View>
|
|
1011
|
+
) : null}
|
|
1012
|
+
</View>
|
|
1013
|
+
);
|
|
1014
|
+
});
|
package/src/Message.tsx
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { forwardRef, type ReactNode } from "react";
|
|
2
|
+
import type { StyleProp, ViewStyle } from "react-native";
|
|
3
|
+
|
|
4
|
+
import { Avatar } from "./Display";
|
|
5
|
+
import { colors, radii } from "./internal/tokens";
|
|
6
|
+
import { primitives } from "./internal/primitives";
|
|
7
|
+
import { useIsDark } from "./internal/motion";
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// MessageBubble
|
|
11
|
+
|
|
12
|
+
export interface MessageBubbleProps {
|
|
13
|
+
/** Whether the message is the device owner's (right, tinted) or not (left). */
|
|
14
|
+
outgoing?: boolean;
|
|
15
|
+
children?: ReactNode;
|
|
16
|
+
style?: StyleProp<ViewStyle>;
|
|
17
|
+
isDark?: boolean;
|
|
18
|
+
testID?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* One message's bubble.
|
|
23
|
+
*
|
|
24
|
+
* Both directions are flat tints, not glass: a chat transcript is a long
|
|
25
|
+
* scrolling list, and glass under every line would stack translucent surfaces
|
|
26
|
+
* over each other and over the conversation behind them. The glass in a chat
|
|
27
|
+
* belongs to the composer, which floats over the transcript — so
|
|
28
|
+
* `NativeAIInput` is glass and the bubbles are not.
|
|
29
|
+
*
|
|
30
|
+
* Incoming messages take the theme's wash; outgoing take `glassActive`, the
|
|
31
|
+
* same tint the library uses for any selected surface, so a transcript reads
|
|
32
|
+
* consistently with the rest of Glacia.
|
|
33
|
+
*/
|
|
34
|
+
export const MessageBubble = forwardRef<any, MessageBubbleProps>(function MessageBubble(
|
|
35
|
+
{ outgoing = false, children, style, isDark: isDarkProp, testID, ...props },
|
|
36
|
+
ref,
|
|
37
|
+
) {
|
|
38
|
+
const isDark = useIsDark(isDarkProp);
|
|
39
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
40
|
+
const { View } = primitives();
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<View
|
|
44
|
+
ref={ref}
|
|
45
|
+
testID={testID}
|
|
46
|
+
style={{
|
|
47
|
+
alignSelf: outgoing ? "flex-end" : "flex-start",
|
|
48
|
+
maxWidth: "82%",
|
|
49
|
+
paddingHorizontal: 12,
|
|
50
|
+
paddingVertical: 8,
|
|
51
|
+
borderRadius: radii.lg,
|
|
52
|
+
backgroundColor: outgoing ? theme.glassActive : theme.wash,
|
|
53
|
+
...(style as ViewStyle),
|
|
54
|
+
}}
|
|
55
|
+
{...props}
|
|
56
|
+
>
|
|
57
|
+
{children}
|
|
58
|
+
</View>
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// MessageAttachment
|
|
64
|
+
|
|
65
|
+
export interface MessageAttachmentProps {
|
|
66
|
+
/** The file's name, shown and used as the accessible label. */
|
|
67
|
+
name: string;
|
|
68
|
+
/** A human-readable size, e.g. "1.2 MB". */
|
|
69
|
+
size?: string;
|
|
70
|
+
/** A leading node, typically an SVG icon. */
|
|
71
|
+
icon?: ReactNode;
|
|
72
|
+
/** Makes the chip tappable, e.g. to open the file. */
|
|
73
|
+
onPress?: () => void;
|
|
74
|
+
disabled?: boolean;
|
|
75
|
+
style?: StyleProp<ViewStyle>;
|
|
76
|
+
isDark?: boolean;
|
|
77
|
+
testID?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A file chip for a message.
|
|
82
|
+
*
|
|
83
|
+
* A flat, bordered chip rather than a glass one, for the same reason the
|
|
84
|
+
* bubbles are flat: it sits inside a bubble or beside one, and glass inside
|
|
85
|
+
* glass is never allowed. The size is a caller-formatted string — byte
|
|
86
|
+
* formatting belongs to the app's locale, not to a component library.
|
|
87
|
+
*/
|
|
88
|
+
export const MessageAttachment = forwardRef<any, MessageAttachmentProps>(
|
|
89
|
+
function MessageAttachment(
|
|
90
|
+
{ name, size, icon, onPress, disabled = false, style, isDark: isDarkProp, testID, ...props },
|
|
91
|
+
ref,
|
|
92
|
+
) {
|
|
93
|
+
const isDark = useIsDark(isDarkProp);
|
|
94
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
95
|
+
const { View, Text, Pressable } = primitives();
|
|
96
|
+
|
|
97
|
+
const body = (
|
|
98
|
+
<>
|
|
99
|
+
{icon ? <View style={{ marginRight: 8 }}>{icon}</View> : null}
|
|
100
|
+
<View style={{ flexShrink: 1 }}>
|
|
101
|
+
<Text numberOfLines={1} style={{ fontSize: 14, color: theme.foreground }}>
|
|
102
|
+
{name}
|
|
103
|
+
</Text>
|
|
104
|
+
{size ? (
|
|
105
|
+
<Text style={{ fontSize: 11, color: theme.muted }}>{size}</Text>
|
|
106
|
+
) : null}
|
|
107
|
+
</View>
|
|
108
|
+
</>
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const chipStyle: ViewStyle = {
|
|
112
|
+
flexDirection: "row",
|
|
113
|
+
alignItems: "center",
|
|
114
|
+
paddingHorizontal: 10,
|
|
115
|
+
paddingVertical: 7,
|
|
116
|
+
borderRadius: radii.md,
|
|
117
|
+
backgroundColor: theme.wash,
|
|
118
|
+
borderWidth: 0.5,
|
|
119
|
+
borderColor: theme.separator,
|
|
120
|
+
opacity: disabled ? 0.38 : 1,
|
|
121
|
+
...(style as ViewStyle),
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
if (!onPress) {
|
|
125
|
+
return (
|
|
126
|
+
<View ref={ref} testID={testID} accessibilityRole="text" accessibilityLabel={size ? `${name}, ${size}` : name} style={chipStyle} {...props}>
|
|
127
|
+
{body}
|
|
128
|
+
</View>
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<Pressable
|
|
134
|
+
ref={ref}
|
|
135
|
+
testID={testID}
|
|
136
|
+
accessibilityRole="button"
|
|
137
|
+
accessibilityLabel={size ? `${name}, ${size}` : name}
|
|
138
|
+
accessibilityState={{ disabled }}
|
|
139
|
+
disabled={disabled}
|
|
140
|
+
onPress={onPress}
|
|
141
|
+
style={chipStyle}
|
|
142
|
+
{...props}
|
|
143
|
+
>
|
|
144
|
+
{body}
|
|
145
|
+
</Pressable>
|
|
146
|
+
);
|
|
147
|
+
},
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
// Message
|
|
152
|
+
|
|
153
|
+
export interface MessageProps {
|
|
154
|
+
/** The sender's name, shown above the content. */
|
|
155
|
+
name?: string;
|
|
156
|
+
/** A time or timestamp, shown beside the name. */
|
|
157
|
+
time?: string;
|
|
158
|
+
/** Draws an Avatar showing `name`'s initials beside the content. */
|
|
159
|
+
avatar?: boolean;
|
|
160
|
+
/** The bubble direction. */
|
|
161
|
+
outgoing?: boolean;
|
|
162
|
+
/** The message's content: text, or a bubble, or a bubble with attachments. */
|
|
163
|
+
children?: ReactNode;
|
|
164
|
+
style?: StyleProp<ViewStyle>;
|
|
165
|
+
isDark?: boolean;
|
|
166
|
+
testID?: string;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* A whole message: who sent it, when, and what they said.
|
|
171
|
+
*
|
|
172
|
+
* The row every chat screen builds by hand — an avatar, a name and time line,
|
|
173
|
+
* and the content under it. Grouping it means the alignment, spacing and
|
|
174
|
+
* accessibility grouping are decided once: the row is announced as a single
|
|
175
|
+
* message (name, time, content) rather than as three unrelated pieces of text.
|
|
176
|
+
*/
|
|
177
|
+
export const Message = forwardRef<any, MessageProps>(function Message(
|
|
178
|
+
{ name, time, avatar = false, outgoing = false, children, style, isDark: isDarkProp, testID, ...props },
|
|
179
|
+
ref,
|
|
180
|
+
) {
|
|
181
|
+
const isDark = useIsDark(isDarkProp);
|
|
182
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
183
|
+
const { View, Text } = primitives();
|
|
184
|
+
|
|
185
|
+
const meta = [name, time].filter(Boolean).join(", ");
|
|
186
|
+
|
|
187
|
+
return (
|
|
188
|
+
<View
|
|
189
|
+
ref={ref}
|
|
190
|
+
testID={testID}
|
|
191
|
+
accessibilityRole="text"
|
|
192
|
+
accessibilityLabel={meta || undefined}
|
|
193
|
+
style={{
|
|
194
|
+
flexDirection: outgoing ? "row-reverse" : "row",
|
|
195
|
+
alignItems: "flex-end",
|
|
196
|
+
gap: 8,
|
|
197
|
+
paddingHorizontal: 4,
|
|
198
|
+
paddingVertical: 4,
|
|
199
|
+
...(style as ViewStyle),
|
|
200
|
+
}}
|
|
201
|
+
{...props}
|
|
202
|
+
>
|
|
203
|
+
{avatar ? (
|
|
204
|
+
<Avatar name={name ?? ""} size="sm" isDark={isDark} />
|
|
205
|
+
) : null}
|
|
206
|
+
<View style={{ flexShrink: 1, alignItems: outgoing ? "flex-end" : "flex-start", gap: 2 }}>
|
|
207
|
+
{name || time ? (
|
|
208
|
+
<View style={{ flexDirection: "row", alignItems: "center", gap: 6 }}>
|
|
209
|
+
{name ? (
|
|
210
|
+
<Text style={{ fontSize: 12, fontWeight: "600", color: theme.foreground }}>{name}</Text>
|
|
211
|
+
) : null}
|
|
212
|
+
{time ? <Text style={{ fontSize: 11, color: theme.muted }}>{time}</Text> : null}
|
|
213
|
+
</View>
|
|
214
|
+
) : null}
|
|
215
|
+
{children}
|
|
216
|
+
</View>
|
|
217
|
+
</View>
|
|
218
|
+
);
|
|
219
|
+
});
|