Jun 25

I am a big fan of both Andrew Dupont, and custom events.

In his presentation he goes through some very nice use cases. Some are cross cutting (e.g. the fact that you can unit test, or debug, or … so much easier) and some are specific such as:

Scripty2 animation heartbeat

JAVASCRIPT:

// keep the heartbeat going
setTimeout(function() {
  document.fire(“effect:heartbeat”);
}, 0);

// listen in
document.observe(“effect:heartbeat”, advanceEffectByOneFrame);

// allows for nice debugging
document.observe(“keydown”, function(event) {
  if (event.keyCode === Event.KEY_RIGHT) {
    document.fire(“effect:heartbeat”);
  }
});
 

Another nice example is how you can start using the new and cool EventSource while retrofitting the functionality for browsers who don’t implement the new standard:

JAVASCRIPT:

// new browsers
var eventSource = $(‘event_source’);
eventSource.observe(‘server-sent-event-name’, function(event) {
  document.fire(‘data:received’, event.data);
});

// old browsers
new Ajax.Request(‘/legacy/polling’, {
  onComplete: function(request) {
    document.fire(‘data:received’, request.responseJSON);
  }
});

// observer works for both
$(document).observe(‘data:received’, function(event) {
  doStuff(event.memo);
});
 

And, there are many more great examples in the slides.

How have you been using custom events?

Continue reading »

Tagged with:
Feb 24

In my never ending quest to find weird and wonderful ways to abuse CSS and all its little intricacies, I have come up with a pretty good way of using CSS to create custom radio and checkbox inputs without JavaScript, that are accessible, keyboard controlled, don’t use any hacks and degrade nicely in non supporting browsers. The journey wasn’t easy and I was on the brink of filing it in the “to crazy” folder, never to be seen again. Luckily I had a brain wave that paid off and actually allowed this to be a very viable solution that degrades beautifully and works in 80% of the browsers. This is my story.

customcssinputs

Ryan “the CSS Ninja” goes from here to explain his quest which includes fun CSS such as:

CSS:

  1.  
  2. p:not(#foo)> input + label:before
  3. {
  4.     position: absolute;
  5.     z-index: 2;
  6.     left: 16px;
  7.     content: url(radio_normal.png);
  8.     width: 16px;
  9.     height: 16px;
  10.     margin: 0 3px 0 -16px;
  11. }
  12.  
  13. p:not(#foo)> input[type=radio]:checked + label:after {
  14.     content: url(radio_checked.png);
  15. }
  16.  
  17. input[type=radio]:hover + label:after,
  18. input[type=radio]:focus + label:after {
  19.     content: url(radio_normal_hover.png);
  20. }
  21. input[type=radio]:hover:checked + label:after,
  22. input[type=radio]:focus:checked + label:after {
  23.     content: url(radio_checked_hover.png);
  24. }
  25. input[type=radio]:hover:disabled + label:after,
  26. input[type=radio]:focus:disabled + label:after {
  27.     content: url(radio_normal_hover_disabled.png);
  28. }
  29. input[type=radio]:hover:checked:disabled + label:after,
  30. input[type=radio]:focus:checked:disabled + label:after {
  31.     content: url(radio_checked_hover_disabled.png);
  32. }
  33. input[type=radio]:active + label:before {
  34.     content: url(radio_normal_active.png);
  35. }
  36. input[type=radio]:active:checked + label:before {
  37.     content: url(radio_checked_active.png);
  38. }
  39.  

He also reminded me of pointer events:

The CSS property pointer-events allows authors to control whether or when an element may be the target of a mouse event. This property is used to specify under which circumstance (if any) a mouse event should go “through” an element and target whatever is “underneath” that element instead.

There is a lot of Ninja in this one. Nice job.

Continue reading »

Tagged with:
preload preload preload