@wistia/ui 3.8.1 → 3.8.2-beta.ce52389c.5eb3c87

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.js CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  /*
3
- * @license @wistia/ui v3.8.1
3
+ * @license @wistia/ui v3.8.2-beta.ce52389c.5eb3c87
4
4
  *
5
5
  * Copyright (c) 2024-2026, Wistia, Inc. and its affiliates.
6
6
  *
@@ -10,7 +10,7 @@
10
10
  import { Tooltip as Tooltip$1 } from "@base-ui/react/tooltip";
11
11
  import { Children, Fragment, cloneElement, createContext, forwardRef, isValidElement, useCallback, useContext, useEffect, useId, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, useTransition } from "react";
12
12
  import { createGlobalStyle, css, keyframes, styled } from "styled-components";
13
- import { isArray, isBoolean, isDate, isEmptyString, isFunction, isNil, isNonEmptyArray, isNonEmptyString, isNotNil, isNotUndefined, isNumber, isRecord, isString, isUndefined, noOpFn } from "@wistia/utils";
13
+ import { hashString, isArray, isBoolean, isDate, isEmptyString, isFunction, isNil, isNonEmptyArray, isNonEmptyString, isNotNil, isNotUndefined, isNumber, isRecord, isString, isUndefined, noOpFn } from "@wistia/utils";
14
14
  import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
15
15
  import { Toaster, toast } from "sonner";
16
16
  import { getValueAndUnit } from "polished";
@@ -1162,7 +1162,6 @@ const getBackgroundGradient = (gradientName) => {
1162
1162
  return isNotNil(gradientName) ? gradients[gradientName] : gradients.defaultDarkOne;
1163
1163
  };
1164
1164
  // src/helpers/getBackgroundGradient/getSemiRandomBackgroundGradient.ts
1165
- const MULTIPLIER = 31;
1166
1165
  const DEFAULT_GRADIENT = "defaultMidOne";
1167
1166
  /**
1168
1167
  * Retrieves a semi-random background gradient based on the given id.
@@ -1172,8 +1171,7 @@ const DEFAULT_GRADIENT = "defaultMidOne";
1172
1171
  */
1173
1172
  const getSemiRandomBackgroundGradient = (id) => {
1174
1173
  if (!(typeof id === "string" && id)) return gradientMap[0] ?? DEFAULT_GRADIENT;
1175
- const hashCode = [...id].reduce((num, char) => Math.imul(MULTIPLIER, num) + (char.codePointAt(0) ?? 0) || 0, 0);
1176
- const index = Math.abs(hashCode) % gradientMap.length;
1174
+ const index = hashString(id) % gradientMap.length;
1177
1175
  return gradientMap[index] ?? DEFAULT_GRADIENT;
1178
1176
  };
1179
1177
  // src/helpers/mergeRefs/mergeRefs.ts
@@ -6308,14 +6306,8 @@ const AvatarWrapper = styled.div`
6308
6306
  }
6309
6307
  `;
6310
6308
  const chooseColorScheme = (name) => {
6311
- if (isNil(name) || name === "Anonymous") return "blue";
6312
- const multiplier = 31;
6313
- const hashCode = (str) => {
6314
- let sum = 0;
6315
- for (let i = 0; i < str.length; i += 1) sum = Math.trunc(Math.imul(multiplier, sum) + (str.codePointAt(i) ?? 0));
6316
- return sum;
6317
- };
6318
- const index = Math.abs(hashCode(name)) % brandColorSchemeOptions.length;
6309
+ if (!isString(name) || name === "Anonymous") return "blue";
6310
+ const index = hashString(name) % brandColorSchemeOptions.length;
6319
6311
  return brandColorSchemeOptions[index] ?? "blue";
6320
6312
  };
6321
6313
  /**
@@ -8501,10 +8493,10 @@ const serializeFormData = (formData) => {
8501
8493
  return data;
8502
8494
  };
8503
8495
  // src/components/Form/Form.tsx
8504
- const StyledForm = styled.form`
8496
+ const StyledForm = styled(Stack)`
8505
8497
  --form-default-width: 690px;
8506
8498
 
8507
- max-width: ${({ $fullWidth }) => $fullWidth ? "auto" : "var(--form-default-width)"};
8499
+ max-width: ${({ $fullWidth }) => $fullWidth ? "none" : "var(--form-default-width)"};
8508
8500
  `;
8509
8501
  const FormContext = createContext({
8510
8502
  values: {},
@@ -8568,7 +8560,7 @@ const Form = ({ children, action, values = DEFAULT_VALUES, labelPosition = "bloc
8568
8560
  ]);
8569
8561
  return /* @__PURE__ */ jsx(FormContext, {
8570
8562
  value: context,
8571
- children: /* @__PURE__ */ jsx(Stack, {
8563
+ children: /* @__PURE__ */ jsx(StyledForm, {
8572
8564
  ...props,
8573
8565
  ref,
8574
8566
  $fullWidth: fullWidth,
@@ -8578,7 +8570,7 @@ const Form = ({ children, action, values = DEFAULT_VALUES, labelPosition = "bloc
8578
8570
  onBlur: handleBlur,
8579
8571
  onReset: handleReset,
8580
8572
  onSubmit: handleSubmit,
8581
- renderAs: StyledForm,
8573
+ renderAs: "form",
8582
8574
  children
8583
8575
  })
8584
8576
  });
@@ -13139,64 +13131,68 @@ const EditableHeading = ({ children, onValueChange, onEditingChange, tooltipText
13139
13131
  }) })] });
13140
13132
  };
13141
13133
  // src/components/EditableText/EditableTextRoot.tsx
13142
- const LARGE_PADDING = "var(--wui-space-02)";
13143
13134
  const SMALL_PADDING = "var(--wui-space-01)";
13144
- const getPaddingForVariant = (variant) => {
13145
- const largePaddingVariants = [
13146
- "hero",
13147
- "heading1",
13148
- "heading2",
13149
- "heading3",
13150
- "body1",
13151
- "label1",
13152
- "body1Mono"
13153
- ];
13154
- const smallPaddingVariants = [
13155
- "heading4",
13156
- "heading5",
13157
- "heading6",
13158
- "body2",
13159
- "body3",
13160
- "body4",
13161
- "label2",
13162
- "label3",
13163
- "label4",
13164
- "body2Mono",
13165
- "body3Mono",
13166
- "body4Mono"
13167
- ];
13168
- if (largePaddingVariants.includes(variant)) return LARGE_PADDING;
13169
- if (smallPaddingVariants.includes(variant)) return SMALL_PADDING;
13170
- return SMALL_PADDING;
13171
- };
13135
+ const LARGE_PADDING = "var(--wui-space-02)";
13136
+ const LARGE_PADDING_VARIANTS = /* @__PURE__ */ new Set([
13137
+ "hero",
13138
+ "heading1",
13139
+ "heading2",
13140
+ "heading3"
13141
+ ]);
13142
+ const getPaddingForVariant = (variant) => LARGE_PADDING_VARIANTS.has(variant) ? LARGE_PADDING : SMALL_PADDING;
13172
13143
  const StyledEditableTextRoot = styled.div`
13173
13144
  display: contents;
13174
13145
 
13175
13146
  --wui-editable-text-padding: ${({ $typographicVariant }) => getPaddingForVariant($typographicVariant)};
13176
13147
  --wui-editable-text-border-radius: var(--wui-border-radius-01);
13148
+
13149
+ /* A negative margin that pulls the display and input out by their padding, so the text
13150
+ * sits where it would without EditableText and doesn't move when editing starts. */
13151
+ --wui-editable-text-bleed: ${({ $bleed }) => $bleed ? "calc(var(--wui-editable-text-padding) * -1)" : "0px"};
13177
13152
  `;
13178
13153
  const EditableTextContext = createContext(null);
13179
- const EditableTextRoot = ({ children, defaultValue = "", value: controlledValue, onValueChange, onValueCommit, onValueRevert, onEditingChange, typographicVariant = "body2", submitMode = "both", readOnly = false, id, label, placeholder = "Click to edit this text", minLines = 1, maxLines, finalFocusEl, ...props }) => {
13154
+ const getEditableTextState = (readOnly, isEditing) => {
13155
+ if (readOnly) return "read-only";
13156
+ if (isEditing) return "editing";
13157
+ return "idle";
13158
+ };
13159
+ /**
13160
+ * Holds the value (controlled or uncontrolled), the value to revert to and the editing state.
13161
+ */
13162
+ const useEditableTextState = ({ controlledValue, defaultValue, onValueChange, onEditingChange }) => {
13180
13163
  const isControlled = controlledValue !== void 0;
13181
13164
  const [internalValue, setInternalValue] = useState(defaultValue);
13182
13165
  const [originalValue, setOriginalValue] = useState(defaultValue);
13183
13166
  const [isEditing, setIsEditing] = useState(false);
13184
13167
  const value = isControlled ? controlledValue : internalValue;
13185
- const generatedId = useId();
13186
- const computedId = isNonEmptyString(id) ? id : `wistia-ui-editable-text-${generatedId}`;
13187
- const handleSetIsEditing = useCallback((editing) => {
13188
- if (editing && !isEditing) setOriginalValue(value);
13189
- setIsEditing(editing);
13190
- onEditingChange?.(editing);
13191
- }, [
13168
+ return {
13192
13169
  isEditing,
13170
+ handleSetIsEditing: useCallback((editing) => {
13171
+ if (editing && !isEditing) setOriginalValue(value);
13172
+ setIsEditing(editing);
13173
+ onEditingChange?.(editing);
13174
+ }, [
13175
+ isEditing,
13176
+ value,
13177
+ onEditingChange
13178
+ ]),
13193
13179
  value,
13180
+ setValue: useCallback((newValue) => {
13181
+ if (!isControlled) setInternalValue(newValue);
13182
+ onValueChange?.(newValue);
13183
+ }, [isControlled, onValueChange]),
13184
+ originalValue
13185
+ };
13186
+ };
13187
+ const EditableTextRoot = ({ children, defaultValue = "", value: controlledValue, onValueChange, onValueCommit, onValueRevert, onEditingChange, typographicVariant = "body2", submitMode = "both", readOnly = false, id, label, placeholder = "Click to edit this text", minLines = 1, maxLines, finalFocusEl, bleed = false, ...props }) => {
13188
+ const { isEditing, handleSetIsEditing, value, setValue, originalValue } = useEditableTextState({
13189
+ controlledValue,
13190
+ defaultValue,
13191
+ onValueChange,
13194
13192
  onEditingChange
13195
- ]);
13196
- const setValue = useCallback((newValue) => {
13197
- if (!isControlled) setInternalValue(newValue);
13198
- onValueChange?.(newValue);
13199
- }, [isControlled, onValueChange]);
13193
+ });
13194
+ const generatedId = useId();
13195
+ const computedId = isNonEmptyString(id) ? id : `wistia-ui-editable-text-${generatedId}`;
13200
13196
  const context = useMemo(() => {
13201
13197
  return {
13202
13198
  isEditing,
@@ -13234,16 +13230,13 @@ const EditableTextRoot = ({ children, defaultValue = "", value: controlledValue,
13234
13230
  maxLines,
13235
13231
  finalFocusEl
13236
13232
  ]);
13237
- const getState = () => {
13238
- if (readOnly) return "read-only";
13239
- if (isEditing) return "editing";
13240
- return "idle";
13241
- };
13242
13233
  return /* @__PURE__ */ jsx(StyledEditableTextRoot, {
13234
+ $bleed: bleed,
13243
13235
  $typographicVariant: typographicVariant,
13244
13236
  "data-testid": "editable-text-root",
13237
+ "data-wui-editable-text-bleed": bleed || void 0,
13245
13238
  "data-wui-editable-text-root": true,
13246
- "data-wui-editable-text-state": getState(),
13239
+ "data-wui-editable-text-state": getEditableTextState(readOnly, isEditing),
13247
13240
  ...props,
13248
13241
  children: /* @__PURE__ */ jsx(EditableTextContext, {
13249
13242
  value: context,
@@ -13253,9 +13246,11 @@ const EditableTextRoot = ({ children, defaultValue = "", value: controlledValue,
13253
13246
  };
13254
13247
  const PolymorphicStyledEditableTextDisplay = withPolymorphicAs(styled.div`
13255
13248
  ${({ $typographicVariant }) => getTypographicStyles($typographicVariant)}
13256
- padding: var(--wui-editable-text-padding);
13249
+ padding-block: var(--wui-editable-text-padding);
13250
+ padding-inline: var(--wui-editable-text-padding);
13257
13251
  border-radius: var(--wui-editable-text-border-radius);
13258
- margin: 0;
13252
+ margin-block: var(--wui-editable-text-bleed, 0);
13253
+ margin-inline: var(--wui-editable-text-bleed, 0);
13259
13254
  transition: all var(--wui-motion-duration-02) var(--wui-motion-ease);
13260
13255
  ${({ $maxLines }) => {
13261
13256
  if (isNotNil($maxLines)) return css`
@@ -13328,88 +13323,206 @@ const EditableTextDisplayComponent = ({ ref, asTrigger, renderAs, ...props }) =>
13328
13323
  };
13329
13324
  const EditableTextDisplay = makePolymorphic(EditableTextDisplayComponent);
13330
13325
  // src/components/EditableText/EditableTextInput.tsx
13326
+ const StyledInputWrapper = styled.div`
13327
+ display: flex;
13328
+ min-width: 0;
13329
+
13330
+ & > [data-wui-input-container] {
13331
+ flex: 1 1 auto;
13332
+ min-width: 0;
13333
+ }
13334
+ `;
13331
13335
  const StyledInput = styled(Input)`
13332
13336
  && {
13333
13337
  ${({ $minLines }) => isNotNil($minLines) && `min-height: calc(${$minLines}lh + calc(var(--wui-editable-text-padding) * 2));`}
13334
13338
  ${({ $maxLines }) => isNotNil($maxLines) && `max-height: calc(${$maxLines}lh + calc(var(--wui-editable-text-padding) * 2));`}
13335
13339
  ${({ $typographicVariant }) => getTypographicStyles($typographicVariant)}
13336
- background-color: var(--wui-color-bg-surface);
13340
+ /* No fill: the text sits on the page as it does when reading, and the ring marks the field */
13341
+ background-color: transparent;
13342
+
13343
+ /* With no fill of its own, the text takes the surrounding color and alignment, like the display */
13344
+ color: inherit;
13345
+ text-align: inherit;
13337
13346
  border-radius: var(--wui-editable-text-border-radius);
13338
- padding: var(--wui-editable-text-padding);
13347
+ padding-block: var(--wui-editable-text-padding);
13348
+ padding-inline: var(--wui-editable-text-padding);
13349
+ margin-block: var(--wui-editable-text-bleed, 0);
13350
+ margin-inline: var(--wui-editable-text-bleed, 0);
13351
+ width: calc(100% - (var(--wui-editable-text-bleed, 0px) * 2));
13339
13352
  resize: none;
13353
+
13354
+ /*
13355
+ * Size to the text, like the display, instead of the textarea's default 20-column width.
13356
+ * Otherwise the field changes width when editing starts wherever its parent shrink-wraps.
13357
+ */
13358
+ field-sizing: content;
13359
+
13360
+ /*
13361
+ * maxLines={1}: behave like a single-line text input. The value stays on one line and
13362
+ * scrolls sideways instead of wrapping and scrolling vertically inside a one-line box.
13363
+ */
13364
+ ${({ $maxLines }) => $maxLines === 1 && css`
13365
+ white-space: pre;
13366
+ overflow-x: auto;
13367
+ overflow-y: hidden;
13368
+ scrollbar-width: none;
13369
+
13370
+ &::-webkit-scrollbar {
13371
+ display: none;
13372
+ }
13373
+ `}
13374
+
13375
+ /* Same color as the display's placeholder, so it doesn't change when editing starts */
13376
+ &::placeholder {
13377
+ color: var(--wui-color-text-secondary);
13378
+ }
13379
+
13380
+ /* Full-perimeter focus ring, inset like Input's so an overflow-hidden ancestor can't clip it */
13381
+ &:focus {
13382
+ outline: 2px solid var(--wui-color-focus-ring);
13383
+ outline-offset: -2px;
13384
+ }
13385
+
13386
+ /*
13387
+ * With submitMode "enter" or "none" the field stays open when focus moves to the
13388
+ * save/cancel buttons. Keep a visible 1px boundary so it still meets non-text contrast.
13389
+ */
13390
+ &:not(:focus) {
13391
+ outline: 1px solid var(--wui-color-border-secondary);
13392
+ outline-offset: -1px;
13393
+ }
13394
+ }
13395
+
13396
+ /*
13397
+ * Input sets its textarea's height from the rows attribute, which would stop the field sizing
13398
+ * to its text. The extra & outranks that rule.
13399
+ */
13400
+ &&& {
13401
+ height: auto;
13340
13402
  }
13341
13403
  `;
13342
- const EditableTextInput = (props) => {
13404
+ const KEYBOARD_HINTS = {
13405
+ both: "Press Enter to save or Escape to cancel. Changes also save when you leave the field.",
13406
+ enter: "Press Enter to save or Escape to cancel.",
13407
+ blur: "Changes save when you leave the field. Press Escape to cancel.",
13408
+ none: "Press Escape to cancel."
13409
+ };
13410
+ /** Whether this key press submits: Enter without Shift, in a submit mode that uses Enter */
13411
+ const isCommitEnter = (event, submitMode) => event.key === "Enter" && !event.shiftKey && (submitMode === "enter" || submitMode === "both");
13412
+ /** A single-line field can't hold line breaks, so each one becomes a space */
13413
+ const toSingleLine = (text) => text.replaceAll(/\r\n|\r|\n/g, " ");
13414
+ /** Whether the browser sizes the textarea to its content itself (`field-sizing: content`) */
13415
+ const supportsFieldSizing = () => typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("field-sizing", "content");
13416
+ /** In a single-line field, scroll sideways so the end of the value (and a caret there) is visible */
13417
+ const scrollToEnd = (element) => {
13418
+ element.scrollLeft = element.scrollWidth;
13419
+ };
13420
+ const EditableTextInput = ({ autoSelect = false, ...props }) => {
13343
13421
  const context = useContext(EditableTextContext);
13344
13422
  if (!context) throw new Error("EditableTextInput must be used within an EditableTextRoot context");
13345
13423
  const { isEditing, value, setValue, onValueCommit, setIsEditing, typographicVariant, submitMode, originalValue, onValueRevert, id, placeholder, minLines, maxLines, finalFocusEl } = context;
13346
13424
  const inputRef = useRef(null);
13425
+ const isEnteringRef = useRef(false);
13426
+ useEffect(() => {
13427
+ isEnteringRef.current = isEditing;
13428
+ }, [isEditing]);
13347
13429
  useEffect(() => {
13348
13430
  if (inputRef.current) {
13349
- if (isEditing) {
13431
+ if (isEditing && !supportsFieldSizing()) {
13350
13432
  const element = inputRef.current;
13351
13433
  const { style } = element;
13352
13434
  style.height = "0px";
13353
13435
  const { scrollHeight } = element;
13354
13436
  style.height = `${scrollHeight}px`;
13355
13437
  }
13356
- if (isEditing) inputRef.current.focus();
13438
+ if (isEditing) {
13439
+ inputRef.current.focus();
13440
+ isEnteringRef.current = false;
13441
+ }
13357
13442
  }
13358
13443
  }, [value, isEditing]);
13444
+ const isSingleLine = maxLines === 1;
13445
+ const fieldValue = isSingleLine ? toSingleLine(value) : value;
13359
13446
  const handleChange = (event) => {
13360
- setValue(event.target.value);
13447
+ const { target } = event;
13448
+ const nextValue = target.value;
13449
+ if (!isSingleLine) {
13450
+ setValue(nextValue);
13451
+ return;
13452
+ }
13453
+ const singleLineValue = toSingleLine(nextValue);
13454
+ if (singleLineValue !== nextValue) {
13455
+ const { selectionStart, selectionEnd } = target;
13456
+ target.value = singleLineValue;
13457
+ target.setSelectionRange(selectionStart, selectionEnd);
13458
+ }
13459
+ setValue(singleLineValue);
13460
+ };
13461
+ const closeAndRestoreFocus = () => {
13462
+ setIsEditing(false);
13463
+ setTimeout(() => {
13464
+ finalFocusEl?.()?.focus();
13465
+ }, 0);
13361
13466
  };
13362
13467
  const handleKeyDown = (event) => {
13363
- if ((submitMode === "enter" || submitMode === "both") && event.key === "Enter" && !event.shiftKey) {
13364
- event.preventDefault();
13365
- onValueCommit?.(value);
13366
- setIsEditing(false);
13367
- setTimeout(() => {
13368
- const element = finalFocusEl?.();
13369
- if (element) element.focus();
13370
- }, 0);
13371
- }
13372
13468
  if (event.key === "Escape") {
13373
13469
  event.preventDefault();
13374
13470
  setValue(originalValue);
13375
13471
  onValueRevert?.(originalValue);
13376
- setIsEditing(false);
13377
- setTimeout(() => {
13378
- const element = finalFocusEl?.();
13379
- if (element) element.focus();
13380
- }, 0);
13472
+ closeAndRestoreFocus();
13473
+ return;
13381
13474
  }
13475
+ if (isCommitEnter(event, submitMode)) {
13476
+ event.preventDefault();
13477
+ onValueCommit?.(fieldValue);
13478
+ closeAndRestoreFocus();
13479
+ return;
13480
+ }
13481
+ if (isSingleLine && event.key === "Enter") event.preventDefault();
13382
13482
  };
13383
13483
  const handleBlur = () => {
13384
13484
  if (submitMode === "blur" || submitMode === "both") {
13385
- onValueCommit?.(value);
13386
- setIsEditing(false);
13387
- setTimeout(() => {
13388
- const element = finalFocusEl?.();
13389
- if (element) element.focus();
13390
- }, 0);
13485
+ onValueCommit?.(fieldValue);
13486
+ closeAndRestoreFocus();
13391
13487
  }
13392
13488
  };
13489
+ const handleFocus = (event) => {
13490
+ const element = event.currentTarget;
13491
+ const shouldSelect = autoSelect && isEnteringRef.current;
13492
+ isEnteringRef.current = false;
13493
+ const { length } = element.value;
13494
+ element.setSelectionRange(length, length);
13495
+ if (isSingleLine) scrollToEnd(element);
13496
+ if (!shouldSelect && !isSingleLine) return;
13497
+ const valueOnFocus = element.value;
13498
+ requestAnimationFrame(() => {
13499
+ if (shouldSelect && element.value === valueOnFocus) element.select();
13500
+ if (isSingleLine && element.selectionEnd === element.value.length) scrollToEnd(element);
13501
+ });
13502
+ };
13393
13503
  if (!isEditing) return null;
13394
- return /* @__PURE__ */ jsx(StyledInput, {
13504
+ const keyboardHintId = `${id}-keyboard-hint`;
13505
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(StyledInputWrapper, { children: /* @__PURE__ */ jsx(StyledInput, {
13395
13506
  ref: inputRef,
13396
13507
  $maxLines: maxLines ?? "infinity",
13397
13508
  $minLines: minLines,
13398
13509
  $typographicVariant: typographicVariant,
13510
+ "aria-describedby": keyboardHintId,
13399
13511
  "data-wui-editable-text-input": true,
13400
13512
  id,
13401
13513
  onBlur: handleBlur,
13402
13514
  onChange: handleChange,
13403
- onFocus: (event) => {
13404
- const { length } = event.currentTarget.value;
13405
- event.currentTarget.setSelectionRange(length, length);
13406
- },
13515
+ onFocus: handleFocus,
13407
13516
  onKeyDown: handleKeyDown,
13408
13517
  placeholder,
13409
13518
  type: "multiline",
13410
- value,
13519
+ value: fieldValue,
13411
13520
  ...props
13412
- });
13521
+ }) }), /* @__PURE__ */ jsx(ScreenReaderOnly, {
13522
+ "data-wui-editable-text-keyboard-hint": true,
13523
+ id: keyboardHintId,
13524
+ text: KEYBOARD_HINTS[submitMode]
13525
+ })] });
13413
13526
  };
13414
13527
  // src/components/EditableText/EditableTextLabel.tsx
13415
13528
  const EditableTextLabel = ({ ...props }) => {