Skip to content

Commit

Permalink
Wrapped layer renaming input in a form and added non-empty check
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Barklund committed Jun 3, 2022
1 parent e4741b5 commit 9a367ab
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions packages/story-editor/src/components/panels/design/layer/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ const LayerDescription = styled.div`
color: ${({ theme }) => theme.colors.fg.primary};
`;

const LayerInputDescription = styled(LayerDescription)`
const LayerInputForm = styled(LayerDescription).attrs({ as: 'form' })`
overflow: visible;
margin-left: 2px;
`;
Expand Down Expand Up @@ -380,22 +380,25 @@ function Layer({ element }) {
setNewLayerName(layerName);
setRenamableLayer(null);
}
};

if (evt.key === 'Enter') {
setRenamableLayer(null);
const updateLayerName = () => {
setRenamableLayer(null);
const trimmedLayerName = newLayerName.trim();
// Don't update name if trimmed layer name is empty.
// This means that submitting an empty name will still exist renaming,
// and the layer name will revert to whatever it was beforem ignoring the empty input.
if (trimmedLayerName) {
updateElementById({
elementId: element.id,
properties: { layerName: newLayerName },
properties: { layerName: trimmedLayerName },
});
}
};

const handleBlur = () => {
setRenamableLayer(null);
updateElementById({
elementId: element.id,
properties: { layerName: newLayerName },
});
const handleSubmit = (evt) => {
evt.preventDefault();
updateLayerName();
};

// We need to prevent the pointer-down event from propagating to the
Expand All @@ -417,18 +420,19 @@ function Layer({ element }) {
currentPageBackgroundColor={currentPageBackgroundColor}
/>
</LayerIconWrapper>
<LayerInputDescription>
<LayerInputForm onSubmit={handleSubmit}>
<LayerInput
tabIndex={-1}
aria-label={__('Layer Name', 'web-stories')}
value={newLayerName}
onChange={handleChange}
onKeyDown={handleKeyDown}
onBlur={handleBlur}
onBlur={updateLayerName}
onPointerDown={stopPropagation}
hasFocus
/>
</LayerInputDescription>
<button hidden />
</LayerInputForm>
</LayerInputWrapper>
) : (
<LayerButton
Expand Down

0 comments on commit 9a367ab

Please sign in to comment.