Problem/Motivation
from #3390712-71: Add component variants to SDC
name: Card
variants:
primary:
title: Primary
description: ...
props:
kind: button
text: my button
third_prop: value13
secondary:
title: Secondary
description: ...
props:
kind: link
text: my button
third_prop: value32
props: {}
slots: {}Here, variants.<name>.props would be validated against the declared props.*.
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3522644
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
nod_How would it work when you define a value for a prop in a variant, then call the template with a different value for that prop? does the variant setting win over the assigned value? Do we trigger a warning when a prop defined in a variant is assigned when calling a template directly?
Also if we decide that the variant "props" are optional in the yaml definition, we essentially have the same thing as the related issue.
Comment #3
penyaskitoRelated to nod_'s question: how would you relate those variants props to data from Drupal, e.g. a value coming from one entity field? Or ar they expected to always be hard-coded values, and "content" data only used in slots?
Comment #4
alex.skrypnykComment #5
alex.skrypnykComment #6
alex.skrypnykComment #8
supriyaguptaAdd card.imfo.yml
id: card
label: Card
description: A reusable card component
type: component
props:
kind:
type: string
default: button
text:
type: string
default: Default text
examples:
primary:
title: Primary card
description: A card with a button
props:
kind: button
text: Click me
secondary:
title: Secondary card
description: A card with a link
props:
kind: link
text: Go there
then add twig file card.html.twig
{{ text }}
{% elseif kind == 'link' %}
{{ text }}
{% else %}
{{ text }}
{% endif %}
and then render in node or page.html.twig
and render in any page or node twig file
{# Renders with defaults #}
{% render '@your_theme/card' %}
{# Renders with manual override #}
{% render '@your_theme/card' with { kind: 'link', text: 'Manually set' } %}
Comment #9
pdureau commentedSince #3390712: Add component variants to SDC, we have introduced variant in SDC definition:
This simple notation covers the majority of the use cases discovered in public design systems and projects.
However, it fails to express advanced 2 use cases:
When there is no explicit "variant" prop upstream documentation
Variants are called "variant" in upstream documentation and/or code for:
However, they may be called:
When proposing this "advanced notation", @alex.skrypnyk told us "do not expose the variant name as a prop to the component's Twig", so this proposal may allow us to be more faithful to the upstream documentation sending the expected variable to the template instead of variant?
However, the notation looks excessively complicated for renaming "variant" to "kind". Example
When a variant is the combination of 2 or more enums
In Bulma, what we call "variant" can sometimes be understood as a combination of "color" and "version". Example: https://bulma.io/documentation/elements/button/
In this case, the advanced notation proposal looks useful:
Conclusion
So, with this advanced notation, what will be the processing? If the chosen variant as a non empty "props" property, we send the value of the props insetad of the "variant" variable?
It will allow us to mix the simple notation and the advanced one. Example:
If
foois selected,variant = foowill be sent to the template. If bar is selected,color = primaryandversion = darkwill be sent to the template.I am still not 100% convinced by this proposal because it is always possible to pick only one of the enum (
colorORversionin this example) as the "glorified" variant enum. But there is something interesting to explore here anyway.Comment #10
pdureau commentedI have updated the title because the principle of "examples" may be better addressed with a "component stories" API, distinct from component definition.
Comment #11
alex.skrypnyk@pdureau
I think we have clarified that I was talking about stories. I'm not sure why this issue was steered towards extending the variants API. The examples and description was talking about stories and there are references to this issue that are talking about stories as well. Now all those links will lead to this issue and people will be confused.
I do not know what to do now since you have added comments to this issue. Can you move them to another issue?
Comment #12
nod_@supriyagupta: I don't understand why you created a fork and the content of your comment, could you clarify please?
@alex.skrypnyk: When I opened the ticket I did not understand you meant exemples yet, so the retitle is inline with what I had in mind, which are the use cases explained by Pierre above.
Comment #13
alex.skrypnykComment #14
nod_I think that's what finnsky had in mind too
Comment #15
alex.skrypnyk@nod_
During our discussion we established that there were 3 understandings of what "variant" meant:
This issue had "storybook" and "examples" in it and it was linked from other issues as an issue related to defining an API for storybook-style examples (3).
Now this became an issue about (2). I've updated the description to use "variants" instead of "examples" for clarity.
I still want a place to track (3). But I will raise it separately.
Comment #16
alex.skrypnykComment #17
alex.skrypnyk