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

feat: add pseudo style-selectors #466

Closed
wants to merge 10 commits into from

Conversation

lessp
Copy link
Member

@lessp lessp commented Apr 22, 2019

EDIT:

So I went on and implemented active([...]) as well, using the same approach with adding active to the Style-type. Which perhaps is still not ideal, but may be good enough. So in my mind, the extractPseudoStyles-function is the mainly what needs improvements.


Draft for brainstorming

Ok, so this is still a work in progress! I've stumbled upon a few things which I'm still trying to get my head around.

  • Did you have any specific ideas on how to hold state in the Primitives.re @Akin909? I just added something to get it to render hover-styles on screen for now (any text-element that is being hovered will apply the styles)

  • Currently to make the Style.create-function accept the list(textStyleProps) we're forced(?) to add hover to Style.t. This also leads me to add it to applyStyle which I've currently made recursive to loop over all the properties of the hover-styles. Right now we could just return style since it's not used at all.


revery-hover-styles

Eventually closes #352

@lessp lessp changed the title feat: hover-styles feat: pseudo-styles Apr 22, 2019
textProps
| fontProps
| coreStyleProps
| pseudoProps(list(textStyleProps))
Copy link
Member

Choose a reason for hiding this comment

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

should this be all props i.e. on hover can we only change the styles that relate to text elements?

Copy link
Member Author

Choose a reason for hiding this comment

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

My thought was to only accept whatever style-props a Text-element (in this case) can take. But it's possible I'm overlooking some use-cases! 🙂

(I've only begun with the Text-primitive for now, but once there's a solid approach I'll add them to the others as well)

@akinsho
Copy link
Member

akinsho commented Apr 25, 2019

@lessp, I wonder if since the setState is happening inside of a component we could actually just actually use Hooks.state (might not be the exact api of the top of my head) to keep track of the Idle and Hover status

@lessp
Copy link
Member Author

lessp commented Apr 25, 2019

@lessp, I wonder if since the setState is happening inside of a component we could actually just actually use Hooks.state (might not be the exact api of the top of my head) to keep track of the Idle and Hover status

Thanks @Akin909, I'll have another look!

The main-issue I have with how it works now is the extractPseudoStyles-function and the fact I can't figure out a good way to not have to add hover to the Style-type. So if there are any ideas on that let me know! 🙂

@lessp lessp changed the title feat: pseudo-styles feat: add pseudo styles-selectors Apr 25, 2019
@lessp lessp changed the title feat: add pseudo styles-selectors feat: add pseudo style-selectors Apr 25, 2019
@lessp lessp force-pushed the feat/hover-as-styles branch from e261586 to c4e0dfb Compare April 25, 2019 19:42
@akinsho
Copy link
Member

akinsho commented May 8, 2019

@lessp re. having to add hover to Style.t, that seems like the right and tbh inevitable thing. We can't have any value in that list that isn't in the type so I think it should be there so having to add it to the other helper functions also makes sense

@lessp
Copy link
Member Author

lessp commented May 8, 2019

@lessp re. having to add hover to Style.t, that seems like the right and tbh inevitable thing. We can't have any value in that list that isn't in the type so I think it should be there so having to add it to the other helper functions also makes sense

Cool! I'll have another look-over at this and then implement it to the others as well! 🙂

@lessp
Copy link
Member Author

lessp commented May 26, 2019

@Akin909, I made some minor adjustments and added hover-styles to most primitives, I did notice that there's a conflict with the View-primitive rooting from the fact that general styles can be passed to the Input-component and then filtered out respectively to its Primitive via extractViewStyles, which is a very nice feature, so it would be nice to apply a workaround to be able to make it work with the View-primitive as well! 🙂

If someone has any pointers to what direction we could go here, that would be great!

@rudolfs
Copy link

rudolfs commented Oct 26, 2019

I could use this feature in a project that I'm working on right now. Is there anything I can help with to move this along?

@lessp
Copy link
Member Author

lessp commented Oct 26, 2019

I could use this feature in a project that I'm working on right now. Is there anything I can help with to move this along?

I've paused this a bit @rudolfs, since I'm not sure how this will align itself with the new API #489

When working on the Hackernews-example I've resorted to doing something like this:

module InteractableText = {
  type state =
    | Idle
    | Hover
    | Active;

  type action =
    | Idle
    | Hover
    | Active;

  let component = React.component("InteractableText");

  let make =
      (
        ~children: list(React.syntheticElement),
        ~hoverStyles=[],
        ~activeStyles=[],
        ~style=[],
        ~text,
        (),
      ) =>
    component(hooks => {
      let (state, dispatch, hooks) =
        Hooks.reducer(
          ~initialState=Idle,
          (action, _state) =>
            switch (action) {
            | Idle => Idle
            | Hover => Hover
            | Active => Active
            },
          hooks,
        );

      let currentStyle =
        switch (state) {
        | Idle => style
        | Hover => Style.merge(~source=style, ~target=hoverStyles)
        | Active => Style.merge(~source=style, ~target=activeStyles)
        };

      (
        hooks,
        <Text
          onMouseOut={_ => dispatch(Idle)}
          onMouseOver={_ => dispatch(Hover)}
          onMouseDown={_ => dispatch(Active)}
          style=currentStyle
          text
        />,
      );
    });

  let createElement =
      (~children, ~hoverStyles=[], ~activeStyles=[], ~style=[], ~text, ()) =>
    make(~children, ~hoverStyles, ~activeStyles, ~style, ~text, ());
};

<InteractableText 
  style=Style.[color(Colors.blue)] 
  hoverStyles=Style.[color(Colors.red)] 
  text="Hello!" 
/>

@rudolfs
Copy link

rudolfs commented Oct 26, 2019

Gotcha. Thanks for the snippet @lessp!

@lessp
Copy link
Member Author

lessp commented Jun 5, 2020

Closing in favour of a hook-based approach e.g.: #868

I think this is my first PR in Revery. They lure you in...

@lessp lessp closed this Jun 5, 2020
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.

UI Styles: Hover Styles
3 participants