@valu/block-components 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.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { forwardRef, useId, useState, useCallback, useRef, useMemo, useEffect } from 'react';
2
2
  import { QueryClientProvider, QueryClient, useInfiniteQuery, keepPreviousData } from '@tanstack/react-query';
3
- import { VisuallyHidden, Icon, Button, Popover, ToggleControl, Spinner, CheckboxControl } from '@wordpress/components';
3
+ import { VisuallyHidden, Icon, Button, Popover, ToggleControl, Spinner, CheckboxControl, TextControl } from '@wordpress/components';
4
4
  import { __, sprintf, _n } from '@wordpress/i18n';
5
5
  import { search, closeSmall, upload, dragHandle, caution, chevronUp, chevronDown, people } from '@wordpress/icons';
6
6
  import styled from '@emotion/styled';
@@ -2081,7 +2081,14 @@ var Table = styled.table`
2081
2081
  border-collapse: separate;
2082
2082
  border-spacing: 0;
2083
2083
  width: 100%;
2084
- min-width: max-content;
2084
+ /*
2085
+ * Do NOT set \`min-width: max-content\` here. Combined with the
2086
+ * trailing FillerCell's \`width: 100%\`, Chrome's auto table layout
2087
+ * enters a feedback loop and blows the table up to ~1,000,000px,
2088
+ * producing a phantom horizontal scrollbar even when all content
2089
+ * fits. The per-cell min-widths (row header, master, leaf) already
2090
+ * prevent content from squishing, so this rule is unnecessary.
2091
+ */
2085
2092
 
2086
2093
  caption {
2087
2094
  ${visuallyHiddenCss2};
@@ -2837,6 +2844,54 @@ var DocMetaInline = styled.div`
2837
2844
  white-space: nowrap;
2838
2845
  }
2839
2846
  `;
2847
+ var ReorderButtons = styled.span`
2848
+ display: none;
2849
+
2850
+ @container valu-dp-row (min-width: 340px) {
2851
+ display: inline-flex;
2852
+ }
2853
+ `;
2854
+ var LabelEditBody = styled.div`
2855
+ flex: 1;
2856
+ min-width: 0;
2857
+ display: flex;
2858
+ flex-direction: column;
2859
+ gap: 2px;
2860
+ padding: 4px 0;
2861
+
2862
+ .components-base-control,
2863
+ .components-text-control__input {
2864
+ width: 100%;
2865
+ min-width: 0;
2866
+ }
2867
+
2868
+ /* __nextHasNoMarginBottom is not honored by every bundled
2869
+ wp-components version — enforce the no-margin layout here. */
2870
+ .components-base-control.components-base-control {
2871
+ margin-bottom: 0;
2872
+ }
2873
+
2874
+ /* Doubled class beats wp-components' own input rules regardless of
2875
+ stylesheet order (the emotion cache may be prepended). */
2876
+ .components-text-control__input.components-text-control__input {
2877
+ font-size: ${tokens.fontSizeBody};
2878
+ height: 28px;
2879
+ min-height: 28px;
2880
+ padding: 2px 8px;
2881
+ }
2882
+
2883
+ @container valu-dp-row (min-width: ${WIDE_ROW}) {
2884
+ flex-direction: row;
2885
+ align-items: center;
2886
+ gap: ${tokens.spaceMd};
2887
+
2888
+ .components-base-control {
2889
+ flex: 0 1 340px;
2890
+ min-width: 180px;
2891
+ width: auto;
2892
+ }
2893
+ }
2894
+ `;
2840
2895
  var Root3 = styled.div`
2841
2896
  color: ${tokens.fg};
2842
2897
  font-size: ${tokens.fontSizeBody};
@@ -2997,6 +3052,7 @@ var mapMediaToDocumentItem = (raw) => {
2997
3052
  raw
2998
3053
  };
2999
3054
  };
3055
+ var displayTitle = (item) => item.label?.trim() || item.title;
3000
3056
  var dedupeAgainst2 = (items, value, enabled) => {
3001
3057
  if (!enabled || value.length === 0) return items;
3002
3058
  const ids = new Set(value.map((v) => v.id));
@@ -3888,6 +3944,8 @@ var SelectedItem2 = ({
3888
3944
  onRemove,
3889
3945
  onMoveUp,
3890
3946
  onMoveDown,
3947
+ enableLabelEdit = false,
3948
+ onLabelChange,
3891
3949
  render
3892
3950
  }) => {
3893
3951
  const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
@@ -3901,22 +3959,46 @@ var SelectedItem2 = ({
3901
3959
  };
3902
3960
  const isMissing = item.missing === "deleted";
3903
3961
  const missingLabel = labels.missing ?? __("This document is no longer available.", "valu-block-components");
3962
+ const displayName = displayTitle(item);
3904
3963
  const removeLabel = labels.remove ? labels.remove : sprintf(
3905
3964
  /* translators: %s: document title */
3906
3965
  __("Remove %s", "valu-block-components"),
3907
- item.title
3966
+ displayName
3908
3967
  );
3909
3968
  const moveUpLabel = labels.moveUp ? labels.moveUp : sprintf(
3910
3969
  /* translators: %s: document title */
3911
3970
  __("Move %s up", "valu-block-components"),
3912
- item.title
3971
+ displayName
3913
3972
  );
3914
3973
  const moveDownLabel = labels.moveDown ? labels.moveDown : sprintf(
3915
3974
  /* translators: %s: document title */
3916
3975
  __("Move %s down", "valu-block-components"),
3917
- item.title
3976
+ displayName
3918
3977
  );
3919
3978
  const modified = showModifiedDate ? formatModified(item.modified) : "";
3979
+ const isLabelEdit = enableLabelEdit && !isMissing && Boolean(onLabelChange);
3980
+ const [labelDraft, setLabelDraft] = useState(null);
3981
+ const commitTimer = useRef(null);
3982
+ useEffect(
3983
+ () => () => {
3984
+ if (commitTimer.current) {
3985
+ clearTimeout(commitTimer.current);
3986
+ }
3987
+ },
3988
+ []
3989
+ );
3990
+ const normalizeLabel = (value) => {
3991
+ const trimmed = value.trim();
3992
+ return trimmed === "" || trimmed === item.title ? "" : value;
3993
+ };
3994
+ const itemLabelText = labels.itemLabel ?? __("Display name", "valu-block-components");
3995
+ const metaParts = [item.filename];
3996
+ if (showFileSize && item.fileSize) {
3997
+ metaParts.push(formatFileSize(item.fileSize));
3998
+ }
3999
+ if (modified) {
4000
+ metaParts.push(modified);
4001
+ }
3920
4002
  return /* @__PURE__ */ jsxs(
3921
4003
  SelectedRow2,
3922
4004
  {
@@ -3938,9 +4020,45 @@ var SelectedItem2 = ({
3938
4020
  }
3939
4021
  ) : null,
3940
4022
  isMissing ? /* @__PURE__ */ jsx(Icon, { icon: caution, size: 20 }) : showExtensionBadge ? /* @__PURE__ */ jsx(ExtensionBadge, { extension: item.extension }) : null,
3941
- render ? render(item) : /* @__PURE__ */ jsxs(Fragment, { children: [
4023
+ render ? render(item) : isLabelEdit ? /* @__PURE__ */ jsxs(LabelEditBody, { children: [
4024
+ /* @__PURE__ */ jsx(
4025
+ TextControl,
4026
+ {
4027
+ __nextHasNoMarginBottom: true,
4028
+ __next40pxDefaultSize: false,
4029
+ "aria-label": sprintf(
4030
+ /* translators: 1: display-name field label, 2: filename */
4031
+ "%1$s: %2$s",
4032
+ itemLabelText,
4033
+ item.filename
4034
+ ),
4035
+ value: labelDraft ?? item.label ?? item.title,
4036
+ onChange: (value) => {
4037
+ setLabelDraft(value);
4038
+ if (commitTimer.current) {
4039
+ clearTimeout(commitTimer.current);
4040
+ }
4041
+ commitTimer.current = setTimeout(() => {
4042
+ commitTimer.current = null;
4043
+ onLabelChange?.(normalizeLabel(value));
4044
+ }, 300);
4045
+ },
4046
+ onBlur: () => {
4047
+ if (commitTimer.current) {
4048
+ clearTimeout(commitTimer.current);
4049
+ commitTimer.current = null;
4050
+ if (labelDraft !== null) {
4051
+ onLabelChange?.(normalizeLabel(labelDraft));
4052
+ }
4053
+ }
4054
+ setLabelDraft(null);
4055
+ }
4056
+ }
4057
+ ),
4058
+ /* @__PURE__ */ jsx(FilenameText, { title: item.filename, children: metaParts.join(" \xB7 ") })
4059
+ ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
3942
4060
  /* @__PURE__ */ jsxs(DocRowBody, { children: [
3943
- /* @__PURE__ */ jsx(PrimaryText, { muted: isMissing, title: item.title, children: isMissing ? missingLabel : item.title }),
4061
+ /* @__PURE__ */ jsx(PrimaryText, { muted: isMissing, title: displayName, children: isMissing ? missingLabel : displayName }),
3944
4062
  !isMissing ? /* @__PURE__ */ jsx(FilenameText, { title: item.filename, children: item.filename }) : null
3945
4063
  ] }),
3946
4064
  !isMissing && (showFileSize || modified) ? /* @__PURE__ */ jsxs(DocMetaInline, { children: [
@@ -3949,7 +4067,7 @@ var SelectedItem2 = ({
3949
4067
  ] }) : null
3950
4068
  ] }),
3951
4069
  /* @__PURE__ */ jsxs(ActionColumn, { children: [
3952
- orderable ? /* @__PURE__ */ jsxs(Fragment, { children: [
4070
+ orderable ? /* @__PURE__ */ jsxs(ReorderButtons, { children: [
3953
4071
  /* @__PURE__ */ jsx(
3954
4072
  Button,
3955
4073
  {
@@ -4041,7 +4159,8 @@ var SelectedList2 = ({
4041
4159
  showHeader = true,
4042
4160
  showExtensionBadge,
4043
4161
  showFileSize,
4044
- showModifiedDate
4162
+ showModifiedDate,
4163
+ enableItemLabels = false
4045
4164
  }) => {
4046
4165
  const resolved = useResolvedDocuments(value);
4047
4166
  const [activeKey, setActiveKey] = useState(null);
@@ -4056,6 +4175,22 @@ var SelectedList2 = ({
4056
4175
  (key) => onChange(value.filter((i) => i.key !== key)),
4057
4176
  [value, onChange]
4058
4177
  );
4178
+ const setLabel = useCallback(
4179
+ (key, label) => onChange(
4180
+ value.map((i) => {
4181
+ if (i.key !== key) {
4182
+ return i;
4183
+ }
4184
+ if (label === "") {
4185
+ const rest = { ...i };
4186
+ delete rest.label;
4187
+ return rest;
4188
+ }
4189
+ return { ...i, label };
4190
+ })
4191
+ ),
4192
+ [value, onChange]
4193
+ );
4059
4194
  const reorder = useCallback(
4060
4195
  (from, to) => {
4061
4196
  if (from === to) return;
@@ -4104,6 +4239,10 @@ var SelectedList2 = ({
4104
4239
  onRemove: () => remove(item.key),
4105
4240
  ...index > 0 ? { onMoveUp: () => reorder(index, index - 1) } : {},
4106
4241
  ...index < resolved.length - 1 ? { onMoveDown: () => reorder(index, index + 1) } : {},
4242
+ ...enableItemLabels ? {
4243
+ enableLabelEdit: true,
4244
+ onLabelChange: (label) => setLabel(item.key, label)
4245
+ } : {},
4107
4246
  ...renderSelected ? { render: renderSelected } : {}
4108
4247
  },
4109
4248
  item.key
@@ -4126,7 +4265,7 @@ var SelectedList2 = ({
4126
4265
  onDragCancel: () => setActiveKey(null),
4127
4266
  children: [
4128
4267
  /* @__PURE__ */ jsx(SortableContext, { items: ids, strategy: verticalListSortingStrategy, children: /* @__PURE__ */ jsx(SelectedListEl, { role: "list", children: rows }) }),
4129
- /* @__PURE__ */ jsx(DragOverlay, { dropAnimation: defaultDropAnimation, children: activeItem ? /* @__PURE__ */ jsx(DragChip, { title: activeItem.title }) : null })
4268
+ /* @__PURE__ */ jsx(DragOverlay, { dropAnimation: defaultDropAnimation, children: activeItem ? /* @__PURE__ */ jsx(DragChip, { title: displayTitle(activeItem) }) : null })
4130
4269
  ]
4131
4270
  }
4132
4271
  )
@@ -4157,6 +4296,7 @@ var DocumentPicker = ({
4157
4296
  fetchOnOpen = false,
4158
4297
  uniqueItems = true,
4159
4298
  enableMediaLibrary = true,
4299
+ enableItemLabels = false,
4160
4300
  showExtensionBadge = true,
4161
4301
  showFileSize = true,
4162
4302
  showModifiedDate = false,
@@ -4248,7 +4388,14 @@ var DocumentPicker = ({
4248
4388
  ...allowedExtensions !== void 0 ? { allowedExtensions } : {},
4249
4389
  multiple: false,
4250
4390
  value,
4251
- onSelect: (items) => handleChange([items[items.length - 1]]),
4391
+ onSelect: (items) => {
4392
+ const next = items[items.length - 1];
4393
+ const prev = value[0];
4394
+ const keepLabel = prev?.id === next.id && prev.label !== void 0;
4395
+ handleChange([
4396
+ keepLabel ? { ...next, label: prev.label } : next
4397
+ ]);
4398
+ },
4252
4399
  labels,
4253
4400
  variant: "tertiary",
4254
4401
  label: labels.replace
@@ -4266,7 +4413,8 @@ var DocumentPicker = ({
4266
4413
  showHeader: value.length > 0,
4267
4414
  showExtensionBadge,
4268
4415
  showFileSize,
4269
- showModifiedDate
4416
+ showModifiedDate,
4417
+ enableItemLabels
4270
4418
  }
4271
4419
  )
4272
4420
  ] }) }) });