Skip to content

refactor(web_core): centralize layout mapping logic#1030

Open
ppazosp wants to merge 2 commits intogoogle:mainfrom
ppazosp:refactor/840-centralize-layout-mapping
Open

refactor(web_core): centralize layout mapping logic#1030
ppazosp wants to merge 2 commits intogoogle:mainfrom
ppazosp:refactor/840-centralize-layout-mapping

Conversation

@ppazosp
Copy link
Copy Markdown

@ppazosp ppazosp commented Mar 29, 2026

Description

Creates mapJustify() and mapAlign() functions in renderers/web_core/src/v0_9/common/layout.ts as the single source of truth for layout enum-to-CSS mapping.

  • Removes 6 duplicate implementations across React (basic + minimal), Lit (minimal), and Angular (basic) renderers
  • Fixes a bug in Angular where raw enum values (e.g., spaceBetween) were passed directly to CSS instead of being mapped to valid values (space-between)

Fixes #840

Pre-launch Checklist

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request centralizes the mapJustify and mapAlign layout mapping utilities into the @a2ui/web_core package, removing redundant local implementations across the Angular, Lit, and React renderers. Feedback suggests adding unit tests for these new shared utilities as required by the style guide and considering an object-based mapping approach instead of switch statements to improve maintainability.

Comment on lines +1 to +15
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

According to the repository style guide (line 17), new code changes should be accompanied by tests. These new utility functions are a core part of the layout logic and should have unit tests to verify their mapping behavior, including handling of default and undefined values.

References
  1. If there are code changes, code should have tests. (link)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, added 15 unit tests for both functions in layout.test.ts (470fe86).

Comment on lines +33 to +52
export function mapJustify(value?: string): string {
switch (value) {
case 'center':
return 'center';
case 'end':
return 'flex-end';
case 'spaceAround':
return 'space-around';
case 'spaceBetween':
return 'space-between';
case 'spaceEvenly':
return 'space-evenly';
case 'start':
return 'flex-start';
case 'stretch':
return 'stretch';
default:
return 'flex-start';
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved conciseness and maintainability, you could consider using an object map for this mapping instead of a switch statement. This approach is often more declarative for simple key-value transformations. A similar refactoring can be applied to mapAlign as well.

For example, you could define the maps at the module level and then reference them in the functions:

const justifyMap: Record<string, string> = {
  center: 'center',
  end: 'flex-end',
  spaceAround: 'space-around',
  spaceBetween: 'space-between',
  spaceEvenly: 'space-evenly',
  start: 'flex-start',
  stretch: 'stretch',
};

export function mapJustify(value?: string): string {
  return (value && justifyMap[value]) || 'flex-start';
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, refactored both functions to use Record<string, string> object maps (470fe86).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Centralize Layout Mapping Logic

1 participant