@valu/block-components 0.3.1 → 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';
@@ -2844,6 +2844,54 @@ var DocMetaInline = styled.div`
2844
2844
  white-space: nowrap;
2845
2845
  }
2846
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
+ `;
2847
2895
  var Root3 = styled.div`
2848
2896
  color: ${tokens.fg};
2849
2897
  font-size: ${tokens.fontSizeBody};
@@ -3004,6 +3052,7 @@ var mapMediaToDocumentItem = (raw) => {
3004
3052
  raw
3005
3053
  };
3006
3054
  };
3055
+ var displayTitle = (item) => item.label?.trim() || item.title;
3007
3056
  var dedupeAgainst2 = (items, value, enabled) => {
3008
3057
  if (!enabled || value.length === 0) return items;
3009
3058
  const ids = new Set(value.map((v) => v.id));
@@ -3895,6 +3944,8 @@ var SelectedItem2 = ({
3895
3944
  onRemove,
3896
3945
  onMoveUp,
3897
3946
  onMoveDown,
3947
+ enableLabelEdit = false,
3948
+ onLabelChange,
3898
3949
  render
3899
3950
  }) => {
3900
3951
  const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
@@ -3908,22 +3959,46 @@ var SelectedItem2 = ({
3908
3959
  };
3909
3960
  const isMissing = item.missing === "deleted";
3910
3961
  const missingLabel = labels.missing ?? __("This document is no longer available.", "valu-block-components");
3962
+ const displayName = displayTitle(item);
3911
3963
  const removeLabel = labels.remove ? labels.remove : sprintf(
3912
3964
  /* translators: %s: document title */
3913
3965
  __("Remove %s", "valu-block-components"),
3914
- item.title
3966
+ displayName
3915
3967
  );
3916
3968
  const moveUpLabel = labels.moveUp ? labels.moveUp : sprintf(
3917
3969
  /* translators: %s: document title */
3918
3970
  __("Move %s up", "valu-block-components"),
3919
- item.title
3971
+ displayName
3920
3972
  );
3921
3973
  const moveDownLabel = labels.moveDown ? labels.moveDown : sprintf(
3922
3974
  /* translators: %s: document title */
3923
3975
  __("Move %s down", "valu-block-components"),
3924
- item.title
3976
+ displayName
3925
3977
  );
3926
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
+ }
3927
4002
  return /* @__PURE__ */ jsxs(
3928
4003
  SelectedRow2,
3929
4004
  {
@@ -3945,9 +4020,45 @@ var SelectedItem2 = ({
3945
4020
  }
3946
4021
  ) : null,
3947
4022
  isMissing ? /* @__PURE__ */ jsx(Icon, { icon: caution, size: 20 }) : showExtensionBadge ? /* @__PURE__ */ jsx(ExtensionBadge, { extension: item.extension }) : null,
3948
- 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: [
3949
4060
  /* @__PURE__ */ jsxs(DocRowBody, { children: [
3950
- /* @__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 }),
3951
4062
  !isMissing ? /* @__PURE__ */ jsx(FilenameText, { title: item.filename, children: item.filename }) : null
3952
4063
  ] }),
3953
4064
  !isMissing && (showFileSize || modified) ? /* @__PURE__ */ jsxs(DocMetaInline, { children: [
@@ -3956,7 +4067,7 @@ var SelectedItem2 = ({
3956
4067
  ] }) : null
3957
4068
  ] }),
3958
4069
  /* @__PURE__ */ jsxs(ActionColumn, { children: [
3959
- orderable ? /* @__PURE__ */ jsxs(Fragment, { children: [
4070
+ orderable ? /* @__PURE__ */ jsxs(ReorderButtons, { children: [
3960
4071
  /* @__PURE__ */ jsx(
3961
4072
  Button,
3962
4073
  {
@@ -4048,7 +4159,8 @@ var SelectedList2 = ({
4048
4159
  showHeader = true,
4049
4160
  showExtensionBadge,
4050
4161
  showFileSize,
4051
- showModifiedDate
4162
+ showModifiedDate,
4163
+ enableItemLabels = false
4052
4164
  }) => {
4053
4165
  const resolved = useResolvedDocuments(value);
4054
4166
  const [activeKey, setActiveKey] = useState(null);
@@ -4063,6 +4175,22 @@ var SelectedList2 = ({
4063
4175
  (key) => onChange(value.filter((i) => i.key !== key)),
4064
4176
  [value, onChange]
4065
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
+ );
4066
4194
  const reorder = useCallback(
4067
4195
  (from, to) => {
4068
4196
  if (from === to) return;
@@ -4111,6 +4239,10 @@ var SelectedList2 = ({
4111
4239
  onRemove: () => remove(item.key),
4112
4240
  ...index > 0 ? { onMoveUp: () => reorder(index, index - 1) } : {},
4113
4241
  ...index < resolved.length - 1 ? { onMoveDown: () => reorder(index, index + 1) } : {},
4242
+ ...enableItemLabels ? {
4243
+ enableLabelEdit: true,
4244
+ onLabelChange: (label) => setLabel(item.key, label)
4245
+ } : {},
4114
4246
  ...renderSelected ? { render: renderSelected } : {}
4115
4247
  },
4116
4248
  item.key
@@ -4133,7 +4265,7 @@ var SelectedList2 = ({
4133
4265
  onDragCancel: () => setActiveKey(null),
4134
4266
  children: [
4135
4267
  /* @__PURE__ */ jsx(SortableContext, { items: ids, strategy: verticalListSortingStrategy, children: /* @__PURE__ */ jsx(SelectedListEl, { role: "list", children: rows }) }),
4136
- /* @__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 })
4137
4269
  ]
4138
4270
  }
4139
4271
  )
@@ -4164,6 +4296,7 @@ var DocumentPicker = ({
4164
4296
  fetchOnOpen = false,
4165
4297
  uniqueItems = true,
4166
4298
  enableMediaLibrary = true,
4299
+ enableItemLabels = false,
4167
4300
  showExtensionBadge = true,
4168
4301
  showFileSize = true,
4169
4302
  showModifiedDate = false,
@@ -4255,7 +4388,14 @@ var DocumentPicker = ({
4255
4388
  ...allowedExtensions !== void 0 ? { allowedExtensions } : {},
4256
4389
  multiple: false,
4257
4390
  value,
4258
- 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
+ },
4259
4399
  labels,
4260
4400
  variant: "tertiary",
4261
4401
  label: labels.replace
@@ -4273,7 +4413,8 @@ var DocumentPicker = ({
4273
4413
  showHeader: value.length > 0,
4274
4414
  showExtensionBadge,
4275
4415
  showFileSize,
4276
- showModifiedDate
4416
+ showModifiedDate,
4417
+ enableItemLabels
4277
4418
  }
4278
4419
  )
4279
4420
  ] }) }) });