Skip to content

ui: when blocks inside elements - #3549

Merged
BaseMax merged 4 commits into
mainfrom
feat/ui-when-conditions
Nov 16, 2026
Merged

BaseMax merged 4 commits into
mainfrom
feat/ui-when-conditions

Conversation

@BaseMax

@BaseMax BaseMax commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Adds when cond: blocks inside an element (section 1 of docs/design/UI_STATES_THEMES.md). A block holds style attributes only, including state prefixes such as hover background.

component Seg(label: str):
    picked := false
    big := false
    button:
        background = "blue"
        hover background = "navy"
        padding = 8
        when picked:
            background = "orange"
            hover background = "red"
        end
        when big:
            padding = 24
        end
        when picked and big:
            padding = 40
        end
        click = ():
            picked = not picked
            big = not big
        end
        content = label
    end
end

Persian: وقتی cond: ... پایان.

How it works (deviation from the design)

The design asks for a generated data attribute plus a CSS rule on the web and a patch layer on native. This PR does not do that. The parser lowers every when block into per-attribute conditional values: x = v inside when c becomes x = c ? v : <earlier value of x>, and the else branch is an empty text (no declaration) when the element has no earlier value. A later when wraps the result of the earlier ones, so source order wins; state prefixes keep their own slot, so base, then when in order, then states holds.

Trade-offs:

  • Both targets reuse the existing runtime style values (web salamWsStyle, native SetText), so behaviour is identical and only the scope that reads the condition re-runs.
  • No new CSS: on the web each distinct value becomes a class from the existing dynamic style sheet, not a rule keyed to an attribute. Environment conditions (dark, screen width < n) need real @media and will come in a later PR with their own mapping.
  • Attribute names are matched by their written spelling (case, _, - and spaces ignored). Mixing two aliases of one property (background and background color) gives the duplicate attribute error instead of merging.
  • when is recognised only at the start of a layout line followed by a condition, so when = ... as an attribute or when: as an element keep working. It is not a global keyword.
  • Empty text means "no declaration" for salamWsStyle and ui.Builder.SetText.
  • E175 (when holds style attributes only) is reported for pages; a when with elements inside a component body is not checked yet. E173 (condition must be bool) is not added: the ternary type check reports it.

Tests

  • tests/{en,fa}/page/components/when_style (web output lines), tests/en/page/errors/when_holds_elements (E175).
  • Ran page/components and page/errors with a compiler built from this branch: 1964 passed, 19 failed, all 19 are in tools/ui-gate-known-failures.txt.
  • Native is not verified: every native ui/* test with a dynamic style value is on the known-failures list on main until ui: native page lowering, comma lists, entry errors #3527 lands (the existing ui/en/scope_list fails the same way with this branch's compiler). The native twin test will follow once ui: native page lowering, comma lists, entry errors #3527 is merged.

@BaseMax
BaseMax requested a review from a team as a code owner November 16, 2026 22:23
@vercel

vercel Bot commented Nov 16, 2026

Copy link
Copy Markdown

Deployment failed for project salam with the following error:

Resource is limited - try again in 1 day (more than 100, code: "api-deployments-free-per-day").

Learn More: https://vercel.com/salamlang?upgradeToPro=build-rate-limit

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 4 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e04eb918-6e05-43de-a523-9ae50ddf2c3e
Commits

Reviewing files that changed from the base of the PR and between 0dfe968 and 2c3a484.

Files selected for processing: 14

  • compiler/ast.salam
  • compiler/cli/cli_translate.salam
  • compiler/lexer/lexer_layout.salam
  • compiler/parser/parser_layout.salam
  • compiler/sal_layout/layout_lower.salam
  • compiler/sal_layout/layout_style_js.salam
  • compiler/semantic/sema_layout_check.salam
  • compiler/semantic/sema_layout_fold.salam
  • compiler/tests_port/token_test.salam
  • compiler/token.salam
  • std/gui/ui/builder.salam
  • tests/en/page/components/when_style.salam
  • tests/en/page/errors/when_holds_elements.salam
  • tests/fa/page/components/when_style.salam
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@vercel

vercel Bot commented Nov 16, 2026

Copy link
Copy Markdown

Deployment failed for project salam with the following error:

Resource is limited - try again in 24 hours (more than 100, code: "api-deployments-free-per-day").

Learn More: https://vercel.com/salamlang?upgradeToPro=build-rate-limit

@BaseMax
BaseMax merged commit a32aec4 into main Nov 16, 2026
16 of 18 checks passed
@BaseMax
BaseMax deleted the feat/ui-when-conditions branch November 16, 2026 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant