ryanve / response.js

Responsive design toolkit
http://responsejs.com
Other
801 stars 123 forks source link

Set other classes/properties at breakpoint? #14

Open alanhogan opened 12 years ago

alanhogan commented 12 years ago

There is a case where simply swapping a class name or two and maybe setting/unsetting the placeholder property on input elements would sufficiently mobile-ize a form I have.

I do not want to overwrite the DOM/innerHTML of the form, because if the user re-sizes their window while filling out the form, I understand their data would be lost. (Not to mention it’s a ridiculous HTML weight overhead for what I want to achieve).

Is there a way to do this using response.js? As far as I could tell, either of these would work:

Thoughts?

alanhogan commented 12 years ago

Thoughts?

ryanve commented 12 years ago

Yo watch this video

I think "attr" functionality is a great idea. Something like this I reckon:

Response.create({
    prefix: "placeholder"
  , attr: "placeholder" // < does not exist now but should be easy to implement
  , prop: "width"
  , breakpoints: [0, 481]
});

in order to write html like:

<input placeholder="John Doe"  data-placeholder-481="Mickey Mouse">

Right?

A safe way to do it right now (v 0.7.0) is like:

(function ($, R) {
    $(function () {
        var $input = $('input');
        if ( !$input.length ) { return; }

        // copy initial placeholder values to data-placeholder
        // `R.store(els, key [, attrToReadFrom])`
        R.store($input, 'placeholder', 'placeholder'); // 3rd arg @since 0.7.0

        R.resize(function () {
            $input.each(function () {
                var newVal = R.band(481) ? R.dataset(this, 'placeholder') : 'small';
                null == newVal || $(this).attr('placeholder', newVal);
            });
        });
    });
}(jQuery, Response));

Although if you're not using Response elsewhere then I would just do it all in jQuery:

(function ($, win) {
    $(function () {

        var $input = $('input'), $win = $(win);
        if ( !$input.length ) { return; }

        $input.each(function() {
            this.setAttribute('data-placeholder', this.getAttribute('placeholder') || '');
        });

        $win.resize(function () {
            $input.each(function () {
                var newVal = $win.width() < 481 ? 'small' : this.getAttribute('data-placeholder');
                null == newVal || this.setAttribute('placeholder', newVal);
            });
        });
    });
}(jQuery, window));

Also there is Response.crossover(fn) mentioned towards the end of the video.

alanhogan commented 12 years ago

Hammer of Thor! A video response! 'cool' emoticon You are awesome.

Thanks so much. This will be super handy.

(And sorry for waiting a few days to reply. I was sick last week.)

Some notes:

ryanve commented 12 years ago

@alanhogan Cool / I'll add the attr functionality in soon ( prob. early Sept ). It should be easy enough. There are several issues with the event methods that I'm hoping to have sorted for version 0.8. I agree, the direction etc. makes sense for the crossover. I think it'd also be useful to be able to add a crossover listener w/o having to first set up a breakpoint set via Response.create(). Maybe something like:

Response.crossover('width', [641, 961], function (eventData)  {
    // fires when viewport width crosses 641 or 961
});
alanhogan commented 12 years ago

Ryan, if you’d like to see the code I ended up using here, I’d be happy to email it to you. Figured it might be interesting as you continue to develop the library and consider adding features like the ones you mentioned above. I’m pretty sure I did some silly things, but basically I created a layer over crossover that fires off callbacks based on the breakpoint name(s) that were just crossed. (In my case, it actually fires off mobile-to-tablet and then tablet-to-desktop transitions if the user suddenly resizes from mobile to desktop, so I can think of things like a state machine with no shortcut hops…)

ryanve commented 12 years ago

@alanhogan Sure it's ryanve at gmail if you want, or post the link if it's online. I think I can imagine but it's always good to see exactly how stuff is being used in order to improve it :]