Giter VIP home page Giter VIP logo

svelte-flex's Introduction

svelte-flex

💪 A simple and reusable flexbox component for Svelte.


A simple and reusable flexbox component for Svelte.

  • Sane defaults.
  • Simple API.
  • Reactive props.
  • Minimal size.

Try me on CodeSandbox!

Requirements

This package assumes you are using at least Svelte 4.x, and that is the only major version which is actively tested. It may work in some older versions, however

This package requires at least [email protected] to work, as it makes use of the style directives feature introduced in the 3.46.x version.

Installation

This package is available on NPM, and you can install it with npm or yarn:

npm install -D svelte-flex

yarn add -D svelte-flex

Usage

Import the Flex component and use it in your Svelte project.

<script>
  import Flex from 'svelte-flex';
</script>

<Flex>
  <div>Flexbox child!</div>
  <div>Flexbox child!</div>
  <div>Flexbox child!</div>
</Flex>

Props

All props are optional (as they all have default values).

Prop Possible Values Default Value
direction 'row' | 'column' 'row'
align 'start' | 'center' | 'end' | 'stretch' 'center'
justify 'start' | 'center' | 'end' | 'around' | 'between' | 'evenly' 'center'
reverse true | false false

Check out the test suite if you're unsure what CSS styles are applied by these props.

The props are reactive, meaning that if they are dynamically updated, the component will update in response.

Additional Props

Any additional props you provide to Flex will be spread onto the root element. This includes custom CSS classes, which will be applied after the flex classes:

Input

<Flex class="my-custom-class" aria-label="flex-boi">
  <span>Flex child</span>
</Flex>

HTML Output

<div class="flex flex-direction--row flex-align--center flex-justify--center my-custom-class" aria-label="flex-boi">
  <span>Flex child</span>
</div>

"Why does this exist?"

I get plenty of comments asking about why this package exists and what value it adds, especially when you could just add your own flex styles and be done with it. This package helps do this in a reusable way so you don't have to think about it, as well as making it easier for folks who may not be as familiar with working with flexbox. Just a reminder that ✨you don't have to use this package✨ if it doesn't suit your needs.

Contributing

Feel free to file an issue or open a pull request. Ensure that you add tests for any new functionality.


👌 Built by Dave Lunny.

svelte-flex's People

Contributors

demetrius-mp avatar dependabot[bot] avatar himynameisdave avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

svelte-flex's Issues

Purpose?

I'm curious - what's the added value by using this component rather than just styling html direct?

Support for Typescript?

When trying to use Flex svelte from within typescript, I get this error:

Could not find a declaration file for module 'svelte-flex'. './node_modules/svelte-flex/lib/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/svelte-flex` if it exists or add a new declaration (.d.ts) file containing `declare module 'svelte-flex';`ts(7016)

Maybe I'm doing something wrong?

However, looking at the source, doesn't look like anything is typed.

Use style directives instead of classnames

Svelte v3.46.0 now supports style directives!

I think that this is how we should be styling things for this component going forward. This way all of the unused CSS classes (probably?) end up in the end-user's code bundle.

I say probably because we'll need to play around with this a bit to say for sure, but in theory this should be a much better way to handle this.

Hoping for no API changes to this component, however I think this should still be marked as a breaking change since it will require at least [email protected].

Adding properties not working with Storybook

Version: 1.1.0
I am not sure if there is a problem with Storybook, this module or my setup...

I have a story with a custom component having a Flex component (imported from the package) and another custom component.
It is not possible to set attributes on the Flex component (that uses $$restProps) but it is possible to set on any of my child components (that uses $$restProps).
I am also using svelte-material-UI in my project and the attributes are passed down correctly (seems to use a custom object with $$props).

I do not have any issues when using this module in my svelte-project.

Support inline-flex

It would be nice if inline-flex was supported for those who need it.

I could imagine the best way to do this would be to either:

  • a) add another prop for inline
  • b) export a second InlineFlex component (perhaps using a span as its default element).

Unique selectors for library styling

Working in a larger codebase, you're likely to have a .flex class already. Libraries like this should user something more unique to ensure that there isn't an unintended inheritance.

I'm partial to [data-*] selectors, but a scoped class like svelte-flex__flex would work.

Ideally, it should be a selector which svelte can mangle into something smaller at compile time.

Taken from this comment.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.