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

Move Pagination to CSS framework #1157

Merged
merged 29 commits into from
Oct 22, 2021

Conversation

amitaibu
Copy link
Collaborator

This is a very naive start for #1123

@amitaibu amitaibu changed the title Move Pagintion to CSS framework WIP: Move Pagintion to CSS framework Oct 19, 2021
@amitaibu amitaibu changed the title WIP: Move Pagintion to CSS framework WIP: Move Pagination to CSS framework Oct 19, 2021
Copy link
Collaborator Author

@amitaibu amitaibu left a comment

Choose a reason for hiding this comment

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

I guess at this point I have more questions than answers 😸

IHP/Pagination/ViewFunctions.hs Outdated Show resolved Hide resolved
-- | Options for customizing the render of a pagination
data PaginationView =
PaginationView
{ cssFramework :: !CSSFramework
Copy link
Collaborator Author

@amitaibu amitaibu Oct 19, 2021

Choose a reason for hiding this comment

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

Why do we need a cssFramework property if we also call styledPagination with CSSFramework? Seems that te pattern also with styledFormField

we later end up calling renderedHtml = styledPagination theCSSFramework theCSSFramework paginationView. How many theCSSFramework do we need? 😅

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's a bit tricky :) Basically we simulate late-binding from OOP here.

This code:

renderedHtml = styledPagination theCSSFramework theCSSFramework paginationView

Can also be written using get:

renderedHtml = (get #styledPagination theCSSFramework) theCSSFramework paginationView

That's important to understand here. We get a styledPagination function using get #styledPagination theCSSFramework.

Then we apply theCSSFramework to that function. We apply the theCSSFramework to that function because the styledPagination function internally might want to call other functions of the CSSFramework type. But you might want to override some functions of the CSSFramework.

Here's an example what would if we don't pass this a second time:

data CSSFramework = CSSFramework { styledPagination :: Html, styledButton :: Html }

bootstrapCSS = CSSFramework { styledPagination, styledButton }
  where
    styledPagination = [hsx|<div>{styledButton}</div>|]
    styledButton = [hsx|<button style="color: red">button</button>|]]

myPage = [hsx|{styledPagination bootstrapCSS}|]

All good. But now as a user we want to override the button styling:

customCSS = bootstrapCSS { styledButton = [hsx|<button style="color: green">button</button>|]]  }

myPage = [hsx|{styledPagination customCSS}|]

When we now render the myPage we will get <div><button style="color: red">button</button></div> (a red button, while our customCSS specified it should be green).

We can fix this by late-binding the calls by manually passing around a CSSFramework. Here I added a CSSFramework to all functions:

data CSSFramework = CSSFramework { styledPagination :: CSSFramework -> Html, styledButton :: CSSFramework -> Html }

bootstrapCSS = CSSFramework { styledPagination, styledButton }
  where
    styledPagination cssFramework = [hsx|<div>{get #styledButton cssFramework}</div>|]
    styledButton cssFramework = [hsx|<button style="color: red">button</button>|]]

myPage = [hsx|{styledPagination bootstrapCSS bootstrapCSS}|]

Now we can customize it again:

customCSS = bootstrapCSS { styledButton = \cssFramework -> [hsx|<button style="color: green">button</button>|]]  }

myPage = [hsx|{styledPagination customCSS customCSS}|]

I hope this clears things up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Super helpful, thanks - In fact I'll copy/ paste this explanation to CSSFramework.hs

Main.hs Show resolved Hide resolved
IHP/View/CSSFramework.hs Outdated Show resolved Hide resolved
@amitaibu
Copy link
Collaborator Author

Update: Got everything working via CSS framework 😄

image

Need to cleanup and adapt to TW as-well

@amitaibu amitaibu changed the title WIP: Move Pagination to CSS framework Move Pagination to CSS framework Oct 21, 2021
@@ -155,6 +164,79 @@ instance Default CSSFramework where

styledSubmitButtonClass = ""

styledPagination :: CSSFramework -> PaginationView -> Blaze.Html
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@mpscholten this default implementation is very much Bootstrap framework, however I don't see much value at having a stripped of classes version, as it will never be used as-is. Do you agree?

So bootstrap function will have no change, only tailwind will

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Another option that might be better is to break it down into smaller pieces, so bootsrap and tailwind will just have to define the classes.

Copy link
Member

Choose a reason for hiding this comment

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

this default implementation is very much Bootstrap framework, however I don't see much value at having a stripped of classes version, as it will never be used as-is. Do you agree?

Agree

Another option that might be better is to break it down into smaller pieces, so bootsrap and tailwind will just have to define the classes.

I just checked the tailwind docs and it looks like tailwind has a completly different dom structure for pagination. So I'd go with the first approach and just make bootstrap the default

@amitaibu
Copy link
Collaborator Author

Tailwind Pagination ✌️

Mobile

image

Desktop

image

@amitaibu amitaibu marked this pull request as ready for review October 21, 2021 11:11
@@ -155,8 +165,91 @@ instance Default CSSFramework where

styledSubmitButtonClass = ""

styledPagination :: CSSFramework -> PaginationView -> Blaze.Html
styledPagination _ paginationView =
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a couple of tests to the new functions? https://github.com/digitallyinduced/ihp/blob/master/Test/View/CSSFrameworkSpec.hs :)

@amitaibu
Copy link
Collaborator Author

Added tests. When merging probably better to squash commits, as so many commits in the PR are breaking

@@ -40,7 +40,7 @@ Install Tailwind via NPM as usual:

```bash
npm init
npm add tailwindcss postcss autoprefixer
npm add tailwindcss postcss autoprefixer @tailwindcss/forms
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes TW behave better with forms, so gets the Pagination look better as-well

Copy link
Member

@mpscholten mpscholten left a comment

Choose a reason for hiding this comment

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

Good job! :)

@mpscholten mpscholten merged commit 46c6c12 into digitallyinduced:master Oct 22, 2021
@amitaibu amitaibu deleted the pagintion-cssframework1 branch October 22, 2021 08:45
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

2 participants