nunomaduro / termwind

🍃 In short, it's like Tailwind CSS, but for the PHP command-line applications.
MIT License
2.29k stars 78 forks source link

Add a `div` to render Inline type components #34

Closed xiCO2k closed 3 years ago

xiCO2k commented 3 years ago

After checking on possible features to add, figured that would be awesome to have the capability to style multiple parts of the same span like its possible to do in HTML with multiple <span> inside of each other.

From this code:


use function Termwind\{span,a};

span([
    span('Termind', 'ml-3 px-1 font-bold text-color-white bg-red'),
    span('it\'s like Tailwind CSS, but for the PHP command-line applications.', 'px-1'),
    a('Check it out on GitHub', 'mr-3 text-color-blue')->href('https://github.com/nunomaduro/termwind'),
])->render();

Output

image

There is some edge cases still need to be handle. but want to know what do you think about this.

nunomaduro commented 3 years ago

That's not what the current render function does?

xiCO2k commented 3 years ago

Render does like this:

image

Code:

render([
    span('Termind', 'ml-3 px-1 font-bold text-color-white bg-red'),
    span('it\'s like Tailwind CSS, but for the PHP command-line applications.', 'px-1'),
    a('Check it out on GitHub', 'mr-3 text-color-blue')->href('https://github.com/nunomaduro/termwind'),
]);
xiCO2k commented 3 years ago

Actually this works from render:

render([[
    span('Termind', 'ml-3 px-1 font-bold text-color-white bg-red'),
    span('it\'s like Tailwind CSS, but for the PHP command-line applications.', 'px-1'),
    a('Check it out on GitHub', 'mr-3 text-color-blue')->href('https://github.com/nunomaduro/termwind'),
]]);

So, maybe add just a renderLine method?

nunomaduro commented 3 years ago

I don't know... I feel that we should add something that groups elements. Like a div.

ju5t commented 3 years ago

I think it would be cool to have an API that accepts the return value of a single element, but also accepts an array of multiple elements. I've kept the naming of all methods neutral :)

So for a single line with one element:

render(element('text', 'style'))

And for multiple element on a single line:

render([
    element('text', 'style'), 
    element('text', 'style'),
])

One thing I do want to say about html-tags vs. generic method names: if you want to stick to html-tags, a span should never include a line break. It's an inline-block. In my example element would be a span and render a div.

This would mean that if you wouldn't use render (or div for that matter) a span will always imply that the next element will be on the same line. And a div wouldn't, as that's a block by default.

edit: to be sure, if you want to call it a span, for me that implies that it does not include multiple elements. Only render or div would. But render or div should be usable on its own, too, in case a line break is required.

xiCO2k commented 3 years ago

@nunomaduro I will implement the div method, that will allow a and span elements on a single terminal line. do you think that is a good idea?

xiCO2k commented 3 years ago

Added a div function and also an abstract BlockElement class to handle more cases that will work with another elements that have the display: block like h1.

How it works:

use function Termwind\{render, div, span, a};

div([
    span('Termind', 'ml-3 px-1 font-bold text-color-white bg-red'),
    span('it\'s like Tailwind CSS, but for the PHP command-line applications.', 'px-1'),
    a('Check it out on GitHub', 'mr-3 text-color-blue')->href('https://github.com/nunomaduro/termwind'),
])->render();

// or

render([
    div([
        span('Termind', 'ml-3 px-1 font-bold text-color-white bg-red'),
        span('it\'s like Tailwind CSS, but for the PHP command-line applications.', 'px-1'),
        a('Check it out on GitHub', 'mr-3 text-color-blue')->href('https://github.com/nunomaduro/termwind'),
    ])
]);

To add on future PRs:

ju5t commented 3 years ago

For me personally I would prefer:

div([
    span('Termind', 'ml-3 px-1 font-bold text-color-white bg-red'),
    span('it\'s like Tailwind CSS, but for the PHP command-line applications.', 'px-1'),
    a('Check it out on GitHub', 'mr-3 text-color-blue')->href('https://github.com/nunomaduro/termwind'),
])

So without render().

xiCO2k commented 3 years ago

Also works for me, but to have the same API from the Element probably make sense to have the ->render() method.

and also work on having multiple lines with the render method like this:

render([
    span('Termind', 'ml-3 px-1 font-bold text-color-white bg-red'),
    div([
        span('it\'s like Tailwind CSS, but for the PHP command-line applications.', 'px-1'),
        a('Check it out on GitHub', 'mr-3 text-color-blue')->href('https://github.com/nunomaduro/termwind'),
    ]),
    span('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore mag'),
]);
xiCO2k commented 3 years ago

Thanks @nunomaduro, just checked the changes on the div and it looks great