Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented CSS style bindable #572

Merged
merged 4 commits into from Feb 28, 2018
Merged

Implemented CSS style bindable #572

merged 4 commits into from Feb 28, 2018

Conversation

adamjez
Copy link
Contributor

@adamjez adamjez commented Feb 26, 2018

Added Style-* property group that generates the style knockout binding. I'm not sure what type should I use for the property group, currently there is object.

Usage example:

<div Style-color="{value: Color}" Style-display="{value: Condition ? 'none' : 'inline-block'}"/>
<div style="background-color: green;" Style-width="{value: Width + 'px'}"/>

Resolves #569

Copy link
Member

@exyi exyi left a comment

Choose a reason for hiding this comment

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

Thanks, looks fine (I found just a single glitch).

try
{
var value = GetValue(styleProperty).ToString();
writer.AddStyleAttribute(styleProperty.GroupMemberName, value);
Copy link
Member

Choose a reason for hiding this comment

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

When the value is null or empty, the style property is not set by knockout. The null case is actually consistent with behavior of this, but I think that we could do that without the NullReferenceException ;)

Also note that when the value contains invalid stuff (like a malitious input), the assignment is ignored on client side but can do some harm on the server. I'm not sure if we can do anything about it, but it's worth noting.

@@ -51,6 +51,11 @@ public HtmlGenericControl(string tagName, bool allowImplicitLifecycleRequirement
public static DotvvmPropertyGroup CssClassesGroupDescriptor =
DotvvmPropertyGroup.Register<bool, HtmlGenericControl>("Class-", "CssClasses");

public VirtualPropertyGroupDictionary<object> StyleProperties => new VirtualPropertyGroupDictionary<object>(this, StylePropertiesGroupDescriptor);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rename it to CssStyles - just to make it similar to CssClasses.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, let's be consistent...

var value = GetValue(styleProperty)?.ToString();
if (!string.IsNullOrEmpty(value))
{
writer.AddStyleAttribute(styleProperty.GroupMemberName, value.ToString());
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to call ToString here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I forgot that after the last changes

foreach (var styleProperty in CssStyles.Properties)
{
if (cssStylesBindingGroup == null) cssStylesBindingGroup = new KnockoutBindingGroup();
cssStylesBindingGroup.Add(styleProperty.GroupMemberName, this, styleProperty);
Copy link
Contributor

Choose a reason for hiding this comment

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

This will add hard-coded values to binding group. I think it's not necessary because hard-coded values are added to style attribute and can't be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I changed it accordingly. thank you

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, but it should be good enough to set the nullBindingAction. You don't need to call HasValueBinding.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or do you want to avoid rendering of empty binding group?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. But yes, I wanted to avoid rendering style data-bind when there are only hard-coded values. I didn't know about nullBindingAction and it's somehow strange that you have to allocate delegate to do nothing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. Can you also change CssClasses to not render empty group in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did. Is it okey?

@adamjez
Copy link
Contributor Author

adamjez commented Feb 28, 2018

thanks for review, I'm merging it.

@adamjez adamjez merged commit c36fc4d into master Feb 28, 2018
@quigamdev quigamdev deleted the features/css-style-bindable branch March 11, 2018 01:28
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.

None yet

3 participants